5
$\begingroup$

I have created a logistic regression in R and would like to use the trained model to create an predict function (lets say in Excel). How can I convert the coefficients into a predict equation?

glm(formula = is_bad ~ is_rent + dti + bc_util + open_acc +    pub_rec_bankruptcies + 
chargeoff_within_12_mths, family = binomial, data = df)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-0.8659  -0.5413  -0.4874  -0.4322   2.4289  

Coefficients:
                            Estimate Std. Error  z value Pr(>|z|)    
(Intercept)              -2.9020574  0.0270641 -107.229  < 2e-16 ***
is_rentTRUE               0.3105513  0.0128643   24.141  < 2e-16 ***
dti                       0.0241821  0.0008331   29.025  < 2e-16 ***
bc_util                   0.0044706  0.0002561   17.458  < 2e-16 ***
open_acc                  0.0030552  0.0012694    2.407   0.0161 *  
pub_rec_bankruptcies      0.1117733  0.0163319    6.844 7.71e-12 ***
chargeoff_within_12_mths -0.0268015  0.0564621   -0.475   0.6350    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 173006  on 233017  degrees of freedom
Residual deviance: 170914  on 233011  degrees of freedom
(2613 observations deleted due to missingness)
AIC: 170928

Number of Fisher Scoring iterations: 4
$\endgroup$
3
  • $\begingroup$ It might help if you say what you have tried so that we can see where you are missing something otherwise you are likely to get true but unhelpful responses. $\endgroup$
    – mdewey
    Commented Sep 2, 2016 at 16:13
  • 1
    $\begingroup$ What I have done is created the logistic regression model in R. I can call predict on it, see the coefficients , etc. I dont know where to go from here. $\endgroup$
    – GMarsh
    Commented Sep 2, 2016 at 16:17
  • $\begingroup$ If I understand your question, you are looking for the predict function. This [link|datascienceplus.com/perform-logistic-regression-in-r/] provides you with a clear example. $\endgroup$ Commented Sep 2, 2016 at 16:43

1 Answer 1

10
$\begingroup$

Unfortunately, what you seem to have run was not a logistic regression model. Note that linear regression (i.e., with normally-distributed residuals) is a special case of the generalized linear model. By default, R assumes a call to glm() is requesting that. You can see that you got that at the bottom of your output where it reads "Dispersion parameter for gaussian family...". To get a logistic regression fit, you need to add the argument family=binomial.

From what you have, the prediction equation would be:

$$ \text{is_bad} = 0.05693 + 0.03428 \text{ is_rentTRUE} + 0.002879 \text{ dti} + \varepsilon \\ \text{where }\varepsilon \sim\mathcal N(0, 0.1065742^2) $$

Now, let's assume that you had included the above argument to the function call (i.e., glm(is_bad~is_rent+dti, data=df, family=binomial)). Then we can state how you would convert the same numbers in the pasted output. (Note that they will actually be different numbers when you go back and do this, and moreover, that the numbers / coefficients will have different interpretations!)

In that case, we can start by recognizing that the coefficients are used to recreate what we call the 'linear predictor'. Using it, we can further construct the prediction equation:

\begin{align} \text{linear predictor} &= 0.05693 + 0.03428 \text{ is_rentTRUE} + 0.002879 \text{ dti} \\[7pt] p(\text{is_bad}=\text{TRUE}) &= \frac{\exp(\text{linear predictor})}{1+\exp(\text{linear predictor})} \end{align}

For a more general reference to interpreting R's output for a logistic regression (including interpretations of the coefficients), it may help to read my answer here: Interpretation of R's output for binomial regression.


Update: We can now use your updated model fit to get the equation you want.

\begin{align} \text{linear predictor} &= -2.9020574 + 0.3105513 \text{ is_rentTRUE } + 0.0241821 \text{ dti } + \\ &\quad\quad\, 0.0044706\text{ bc_util } + 0.0030552\text{ open_acc} + \\ &\quad\quad\, 0.1117733\text{ pub_rec_bankruptcies } + \\ &\quad -0.0268015\text{ chargeoff_within_12_mths} \\[7pt] p(\text{is_bad}=\text{TRUE}) &= \frac{\exp(\text{linear predictor})}{1+\exp(\text{linear predictor})} \end{align}

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.