R: predict.lm () does not recognize the object

> reg.len <- lm(chao1.ave ~ lg.std.len, b.div) # b.div is my data frame imported from a CSV file > reg.len Call: lm(formula = chao1.ave ~ lg.std.len, data = b.div) Coefficients: (Intercept) lg.std.len 282.4 -115.7 > newx <- seq(0.6, 1.4, 0.01) > prd.len <- predict(reg.len, newdata=data.frame(x=newx), interval="confidence", level=0.90, type="response") Error in eval(expr, envir, enclos) : object 'lg.std.len' not found 

I tried doing lm like this: lm(b.div$chao1.ave ~ b.div$lg.std.len) , but then predict() gives warnings that newdata and variables are different in length. So, I tried the path above, and now predict() gives an error saying that it does not recognize the object. How to fix please?

+4
source share
1 answer

Predict expects newdata to have the same column names (to match the formula in reg.len). You change it to "x" in your newdata specification, which is not part of the formula.

 dat <- data.frame(y=rnorm(50),lg.std.len=sample(10:15,50,replace=TRUE)) reg.len <- lm(y ~ lg.std.len,data=dat) newx <- seq(0.6, 1.4, 0.01) prd.len <- predict(reg.len, newdata=data.frame(lg.std.len=newx), interval="confidence", level=0.90, type="response") 

The key part is newdata=data.frame(lg.std.len=newx)

+5
source

Source: https://habr.com/ru/post/1440145/


All Articles