Nile
Nile

Reputation: 333

Predicting new data using glm and cv.glmnet in R (including interactions and categorical variables )

I'd like to model a regression formula including interactions ad categorical variables. I am interested to use glm and glmnet::cv.glmnet. I am ok with the functions which fit the model but am not quite sure that I am using the trained models to forecast out sample data correctly. Here is an example.

Formula <- "Sepal.Length ~ Sepal.Width + Petal.Length + as.factor(Species):Petal.Width + Sepal.Width:Petal.Length +  as.factor(Species) +  bs(Petal.Width, df = 2, degree = 2)"

data("iris")
Inx <- sample( 1: nrow(iris), nrow(iris),  replace = F)

iris$Species <- as.factor(iris$Species)

train_data <- iris[Inx[1:100], ]
test_data <- iris[Inx[101:nrow(iris) ],]

#---- glm -----------------
ModelMatrix <- predict(caret::dummyVars(Formula, train_data, fullRank = T, sep = ""), train_data)
glmfit <- glm(formula = as.formula(Formula) , data = train_data)

prd_glm <- predict(glmfit, newx = ModelMatrix, type = "response")

#------- glm cross validation --------------
cvglm <- glmnet::cv.glmnet(x = ModelMatrix,
                           y = train_data$Sepal.Length,
                           nfolds = 4, keep = TRUE, alpha = 1, parallel = F, type.measure = 'mse')

ModelMatrix_test <- predict(caret::dummyVars(Formula, test_data, fullRank = T, sep = ""), test_data)
prd_cvglm <- predict(cvglm, newx = ModelMatrix_test,  s = "lambda.1se", type ='response')

Upvotes: 2

Views: 1196

Answers (1)

StupidWolf
StupidWolf

Reputation: 46948

You either use a modelmatrix, or a formula, but not both, because once you provide a formula, any glm will internally generates model matrix. And you factor only once. So in your case, let's say directly fit the model matrx:

library(splines)
library(caret)
library(glmnet)

data(iris)
Inx <- sample(nrow(iris),100)
iris$Species <- factor(iris$Species)

train_data <- iris[Inx, ]
test_data <- iris[-Inx,]

Formula <- "Sepal.Length ~ Sepal.Width + Petal.Length + Species:Petal.Width + Sepal.Width:Petal.Length +  Species +  bs(Petal.Width, df = 2, degree = 2)"

glmfit <- glm(as.formula(Formula),data=train_data)

You can see this is the same as fitting with a formula:

ModelMatrix <- predict(caret::dummyVars(Formula, train_data, fullRank = T, sep = ""), train_data)
y = train_data[,"Sepal.Length"]
fit_dummy = glm(y ~ ModelMatrix)
table(fitted(glmfit) == fitted(fit_dummy))
TRUE 
 100

And we predict on the test data:

prd_glm <- predict(glmfit, newdata = test_data, type = "response")

Then for glmnet:

cvglm <- cv.glmnet(x = ModelMatrix,y = train_data$Sepal.Length,nfolds = 4, 
    keep = TRUE, alpha = 1, parallel = F, type.measure = 'mse')

ModelMatrix_test <- predict(caret::dummyVars(Formula, test_data, fullRank = T, sep = ""), test_data)
prd_cvglm <- predict(cvglm, newx = ModelMatrix_test,  s = "lambda.1se", type ='response')

You can see how they differ:

plot(prd_glm,prd_cvglm)

enter image description here

Upvotes: 2

Related Questions