Silva
Silva

Reputation: 153

Linear Regression: Error in eval(predvars, data, env) : object 'G3' not found

I'm new to R. I am trying to use a multiple linear regression algorithm in a data set. The attribute I am trying to predict is named G3. I tried to do this:

d1=read.table("student-mat.csv",sep=";",header=TRUE)

train <- d1[1:356,]
test <- d1[357:395,]
fit2 <- lm(G3 ~ famrel + G1 + G2, data=train)
coefficients(fit2)

It worked with no errors. Then I tried to do cross-validation, so I did this:

install.packages("DAAG")
library(DAAG)
cv.lm( form.lm = fit2, m=3, dots=FALSE) # 3 fold cross-validation

But the last line gave me this error:

Error in eval(predvars, data, env) : object 'G3' not found

I can't understand why. I searched for this error, and it normally happens when the object is not in the data frame, which is not the case. Can someone tell me what can I do?

Upvotes: 3

Views: 6128

Answers (1)

PKumar
PKumar

Reputation: 11128

You seemed to have missing the data argument in cv.lm, that is why R is not able to find G3 object. It should be like below:

library(DAAG)
cv.lm(data= mtcars, mpg ~ drat + hp, m= 3)

I am using here mtcars data, you can try with your data and let me know. It should work

Upvotes: 3

Related Questions