R中的线性回归:变量的无效类型(列表)?

Linear regression in R: invalid type (list) for variable?

t_X <- rbind( c(0.89, 0.46, 0.45, 0.56, 0.41, 0.44, 0.34, 0.74, 0.75, 0.48),
              c(0.02, 0.09, 0.16, 0.09, 0.02, 0.17, 0.23, 0.11, 0.01, 0.15),
              c(0.01, 0.24, 0.23, 0.09, 0.28, 0.14, 0.20, 0.01, 0.15, 0.06),
              c(18.7, 31.3, 30.0, 20.0, 31.5, 22.0, 25.7, 18.7, 27.3, 18.3),
              c(26.8, 33.4, 35.1, 25.7, 34.8, 28.0, 31.4, 26.8, 34.6, 22.8),
              c(42.1, 45.7, 48.3, 39.3, 46.5, 38.5, 41.1, 37.8, 47.8, 32.8),
              c(56.6, 49.3, 53.5, 46.6, 46.7, 46.7, 50.6, 50.6, 55.9, 43.4),
              c(70.0, 53.8, 59.2, 56.5, 48.5, 54.1, 53.5, 65.0, 67.9, 49.6),
              c(83.2, 55.3, 57.7, 57.8, 51.1, 53.6, 49.3, 72.3, 75.2, 51.1))
X <- as.data.frame(t(t_X))
colnames(X) <- c("c1", "c2", "c3", "A1", "A2", "A3", "A4", "A5", "A6")

X.labels <- X[,1:3]
X.training <- X[,4:9]

我正在尝试从 A1-A6 构建 c1、c2、c3 的线性模型。 不幸的是,我收到错误消息:

Error in model.frame.default(formula = X.labels ~ X.training, drop.unused.levels = TRUE) :
  invalid type (list) for variable 'X.labels'

当我做的时候

xlm <- lm(X.labels ~ X.training)

知道为什么吗?

您只需将一个因变量传递给 lm。如果你想要每个 c 的模型,你可以这样做:

xlm <- apply(X.labels,2,function(xl)lm(xl ~.,data= X.training))
xlm

获取:

> xlm
$c1

Call:
lm(formula = xl ~ ., data = X.training)

Coefficients:
(Intercept)           A1           A2           A3           A4           A5  
   0.050096     0.002525    -0.009387     0.003754    -0.009197    -0.001056  
         A6  
   0.017881  


$c2

Call:
lm(formula = xl ~ ., data = X.training)

Coefficients:
(Intercept)           A1           A2           A3           A4           A5  
  0.0266587    0.0066861   -0.0007149   -0.0183789    0.0140998    0.0160385  
         A6  
 -0.0152220  


$c3

Call:
lm(formula = xl ~ ., data = X.training)

Coefficients:
(Intercept)           A1           A2           A3           A4           A5  
  -0.077624     0.001679     0.007541     0.006682     0.002210    -0.005104  
         A6  
  -0.002375