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]
i trying build linear models of c1, c2, c3 form a1-a6. unfortunately, getting error:
error in model.frame.default(formula = x.labels ~ x.training, drop.unused.levels = true) : invalid type (list) variable 'x.labels'
when do
xlm <- lm(x.labels ~ x.training)
any ideas why?
you need pass 1 depended variable lm. if want models each c 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
Comments
Post a Comment