Output Regression statistics for each variable one at a time in R

后端 未结 3 1204
说谎
说谎 2021-01-15 11:35

I have a data frame that looks like this. names and number of columns will NOT be consistent (sometimes \'C\' will not be present, other times \"D\', \'E\', \'F\' may be pre

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-15 12:03

    Here is a solution using *apply:

    Y <- c(4, 4, 3, 4, 3, 2, 3, 2, 2, 3, 4, 4, 3, 4, 8, 6, 5, 4, 3, 6)
    A <- c(1, 2, 1, 2, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1, 2, 2, 1, 2, 4, 8)
    B <- c(5, 6, 6, 5, 3, 7, 2, 1, 1, 2, 7, 4, 7, 8, 5, 7, 6, 6, 4, 7)
    C <- c(9, 1, 2, 2, 1, 4, 5, 6, 7, 8, 89, 9, 7, 6, 5, 6, 8, 9 , 67, 6)
    YABC <- data.frame(Y, A, B, C)
    
    names <- colnames(YABC[-1])
    
    formulae <- sapply(names,function(x)as.formula(paste('Y~',x)))
    
    lapply(formulae, function(x) lm(x, data = YABC))
    

    Of course you can also call summary:

    lapply(formulae, function(x) summary(lm(x, data = YABC)))
    

    If you want to extract variables from a specific model do as follows:

    results <- lapply(formulae, function(x) lm(x, data = YABC))
    results$A$coefficients
    

    gives the coefficients from the model using A as explanatory var

提交回复
热议问题