I have a model in R:
lm(formula = Y ~ rowmeans(df[, c(10:14)]), data=df)
I want to fit this against the means of columns 10:14 so that my output has 5 rows to predict the Y values.
I'm calculating the column means as follows:
t(as.data.frame.list(colMeans(df[, c(10:14)], na.rm=TRUE)))
This is the correct output. However, when passing this into my linear regression predict()
function, I'm not receiving 5 rows as expected.
I'm trying:
fit <- lm(formula = Y ~ rowmeans(df[, c(10:14)]), data=df)
predict(fit, newdata = list(t(as.data.frame.list(colMeans(df[, c(10:14)],
na.rm = TRUE)))))
This produces an output of 300 values...
CodePudding user response:
Do not put complicated stuff in a model formula:
df$x <- rowmeans(df[, 10:14]) ## create variable 'x'
fit <- lm(Y ~ x, data = df)
## provide variable 'x'
newdf <- data.frame(x = colMeans(df[, 10:14], na.rm = TRUE))
predict(fit, newdf)