0
votes

I'm performing some experiments with logistic regression in R with the Auto dataset included in R.

I've get the training part (80%) and the test part (20%) normalizing each part individually.

I can create the model without any problem with the line:

mlr<-glm(mpg ~ 
displacement + horsepower + weight, data =train)

I can even predict train$mpg with the train set:

trainpred<-predict(mlr,train,type="response")

And with this calculate the sample error:

etab <- table(trainpred, train[,1])
insampleerror<-sum(diag(etab))/sum(etab)

The problem comes when I want predict with the test set. I use the following line:

testpred<-predict(model_rl,test,type="response")

Which gives me this warning:

'newdata' had 79 rows but variables found have 313 rows

but it doesn't work, because testpred have the same length of trainpred (should be less). When I want calculate the error in test using testpred with the following line:

etabtest <- table(testpred, test[,1])

I get the following error:

Error en table(testpred, test[, 1]) :
all arguments must have the same length

What I'm doing wrong?

2
One thing that probably won't make a difference: change you regression to mlr<-glm(mpg ~ displacement + horsepower + weight, data =train). You don't need the train$ if you have specified the data argument. More importantly, you might check that this creates a logistic regression. I think it is actually OLS. You have to set the link and family arguments. There are many examples on SO.lmo

2 Answers

0
votes

I response my own question if someone have the same problem:

When I put the arguments in glm I'm saying what I want to predict, this is Auto$mpg labels with train data, hence, my glm call must be:

attach(Auto)
mlr<-glm(mpg ~ 
displacement + horsepower + weight, data=Auto, subset=indexes_train)

If now I call predict, table, etc there isn't any problem of structures sizes. Modifying this mistake it works for me.

0
votes

As imo says: "More importantly, you might check that this creates a logistic regression. I think it is actually OLS. You have to set the link and family arguments."

set familiy = 'binomial'