user1678
user1678

Reputation:

Getting "invalid type (list) for variable" when fitting a logistic regression model

I am trying to fit a logistic regression model with all predictors on training data, but I keep getting errors. I got this:

library(kernlab)
data(spam)

tr_idx = sample(nrow(spam), 1000)
spam_tr = spam[tr_idx,]  # training
spam_te = spam[-tr_idx]  # testing

fit_tr = lm(spam_te ~ spam_tr, data=spam)

but this error always comes out:

Error in model.frame.default(formula = spam_te ~ spam_tr, data = spam, : 
invalid type (list) for variable 'spam_te'

and when I input this:

fit_tr = lm(spam_te ~ spam_tr, data=tri_dx)

I got another error:

Error in is.data.frame(data) : object 'tri_dx' not found

Upvotes: 1

Views: 3606

Answers (2)

Glen
Glen

Reputation: 1772

In the formula you need to specify the variables in the model, not the data sets.

lm is also a linear model, not logistic.

Upvotes: 0

VJ1988
VJ1988

Reputation: 13

There are multiple issues with your code. 1. your third line misses a coma 2. your fourth line needs to have the only spam_tr because a linear model is fitted on training data first and then tested on testing data.

tr_idx = sample(nrow(spam), 1000)
spam_tr = spam[tr_idx,]
spam_te = spam[-tr_idx,]
fit_tr = lm(spam_tr , data = spam)

Hope this helps.

Upvotes: 1

Related Questions