-1
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]

I am trying to build linear models of c1, c2, c3 form A1-A6. Unfortunately, I am getting the error:

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

when I do

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

Any ideas why?

1
  • 3
    You can't pass data.frames in formula like that. Do you want something like lm(cbind(c1,c2,c3)~., X) maybe? Or you can explicitly convert to matrices: xlm <- lm(as.matrix(X.labels) ~ as.matrix(X.training)) (but be careful of any potentially undesired data type casting).
    – MrFlick
    Jun 19, 2015 at 20:00

1 Answer 1

1

You need to pass only one depended variable to lm. If you want models for each c you could do:

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

To get:

> 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.