Error404
Error404

Reputation: 7121

Using predict.glm with a user defined function

I have recently fitted a glm model. I am currently trying to change one of the parameters (intercept for example, while keeping other parameters constant) to see how that affects the prediction. I have thought of two approaches, both of which I have failed at:

Changing the glm model manually (which I couldn't do) or writing a function myself and make it as a glm class

I would like to use my "user-defined" models with predict.glm() to see what how this affects the prediction. An example of a model, similar to mine is given below:

> fit <- glm(formula = am ~ wt+cyl , family = binomial, data = mtcars)
> fit

Call:  glm(formula = am ~ wt + cyl, family = binomial, data = mtcars)

Coefficients:
(Intercept)           wt          cyl  
     15.749       -7.864        1.322  

Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
Null Deviance:      43.23 
Residual Deviance: 14.73        AIC: 20.73

Is there a way how I can edit that "fit" model manually and use it for prediction as a glm? I am not a core statistician, so I hope it all makes sense. Thank you

Upvotes: 3

Views: 831

Answers (2)

polka
polka

Reputation: 1529

When you use glm function, you are creating an instance of the "glm" class and naming it "fit".

> fit <- glm(formula = am ~ wt+cyl , family = binomial, data = mtcars)
> class(fit)
[1] "glm" "lm"

If you want to access any of the parameters of that instance, you use the "$" signifier at the end of the name to access the many different parameters. The "coefficients" is just one of many parameters of the glm class.

> fit$coefficients
(Intercept)          wt         cyl 
   15.74943    -7.86400     1.32173 

If you want to access and change any of the parameters, you can use the assignment "<-" symbol to assign a new value to the parameter.

> fit$coefficients[1]
(Intercept) 
   15.74943 
> fit$coefficients[1]<-15.75
> fit$coefficients
(Intercept)          wt         cyl 
   15.75000    -7.86400     1.32173

If you want to use the adjusted glm function to predict a value, then you can use the same predict function on the new fit.

Statistically, measures of accuracy like the AIC and residuals are calculated at the same time as the model is generated. Manually editing the functions like this means that all these values are not updated and are inaccurate.

Upvotes: 2

maccruiskeen
maccruiskeen

Reputation: 2818

You can just change the coefficients in fit manually and use the predict function.

# Fit the model:
fit <- glm(formula = am ~ wt+cyl , family = binomial, data = mtcars)
# Look at the first few predictions:
head(predict(fit, mtcars))


        Mazda RX4     Mazda RX4 Wag        Datsun 710    Hornet 4 Drive 
        3.0761275         1.0708076         2.7918672        -1.6029523 
Hornet Sportabout           Valiant 
       -0.7288921        -3.5296322 

# Change the intercept to 10 for example:
fit$coefficients[1] <- 10

# Look at the first few predictions again (notice they are different):
print(head(predict(fit, mtcars)))

head(predict(fit, mtcars))
    Mazda RX4     Mazda RX4 Wag        Datsun 710    Hornet 4 Drive 
    -2.673299         -4.678619         -2.957559         -7.352378 
Hornet Sportabout           Valiant 
        -6.478318         -9.279058    

Upvotes: 2

Related Questions