Search code examples
rglmpredict

R predict() dependent variable Y=f(X) using arbitrary coefficients


I am running a logistic regression of the following form in R:

results<-glm(Y~X,family=binomial(link='logit')) from which I derive the estimated coefficients and the fitted observations \hat{Y}=f(X;\hat{\beta}).

I am wondering if it is possible to calculate a different \widetilde{Y} using some other arbitrary fixed value \widetilde{\beta}.

Please note that I have already tried to change the estimated coefficients results$cofficients<-c(0.9,1.2) (here 0.9 and 1.2 are just examples) and use the function predict(results,type="response") although it did not work as there was no difference between the fitted values.


Solution

  • Changing the coefficients should do it. However, if you're predicting with the original data by just relying on the predict function using your original data if not passed any additional data to predict (i.e. not using newdata=something) it uses the fitted values from the model object directly.

    You can bypass this by telling it to use newdata=your_original_data in your call to predict.

    An example

    > dat <- mtcars[1:5,]
    > results <- glm(vs ~ mpg, data = dat, family = binomial(link = 'logit'))
    Warning message:
    glm.fit: fitted probabilities numerically 0 or 1 occurred 
    > predict(results)
            Mazda RX4     Mazda RX4 Wag        Datsun 710    Hornet 4 Drive Hornet Sportabout 
            -23.66411         -23.66411         186.49174          23.03719        -292.19658 
    > coefficients(results)
    (Intercept)         mpg 
     -2475.4823    116.7532 
    > results$coefficients[1] <- 0
    > predict(results) # uses original fitted values
            Mazda RX4     Mazda RX4 Wag        Datsun 710    Hornet 4 Drive Hornet Sportabout 
            -23.66411         -23.66411         186.49174          23.03719        -292.19658 
    > predict(results, newdata = dat)
            Mazda RX4     Mazda RX4 Wag        Datsun 710    Hornet 4 Drive Hornet Sportabout 
             2451.818          2451.818          2661.974          2498.519          2183.286