|
|
|
Roberto Patuelli-2
|
Dear ALL,
I'm trying to figure out what the percentage effects are in a logistic regression. To be more clear, I'm not interested in the effect on y of a 1-unit increase in x, but on the percentage effect on y of a 1% increase in x (in economics this is also often called an "elasticity"). For example, if my independent variables are in logs, the betas can be directly interpreted as percentage effects both in OLS and Poisson regression. What about the logistic regression? Is there a package (maybe effects?) that can compute these automatically? Thanks and best regards, Roberto Patuelli ******************** Roberto Patuelli, Ph.D. Istituto Ricerche Economiche (IRE) (Institute for Economic Research) Università della Svizzera Italiana (University of Lugano) via Maderno 24, CP 4361 CH-6904 Lugano Switzerland Phone: +41-(0)58-666-4166 Fax: +39-02-700419665 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
||||||||||||||||
|
Daniel Malter
|
Somebody might have done this, but in fact it's not difficult to compute the
marginal effects yourself (which is the beauty of R). For a univariate logistic regression, I illustrate two ways to compute the marginal effects (one corresponds to the mfx, the other one to the margeff command in Stata). With the first you compute the marginal effect based on the mean fitted values; with the second you compute the marginal effect based on the fitted values for each observation and then mean over the individual marginal effects. Often the second way is considered better. You can easily extend the R-code below to a multivariate regression. ##### #####Simulate data and run regression ##### set.seed(343) x=rnorm(100,0,1) #linear predictor lp=exp(x)/(1+exp(x)) #probability y=rbinom(100,1,lp) #Bernoulli draws with probability lp #Run logistic regression reg=glm(y~x,binomial) summary(reg) ##### #####Regression output ##### Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.1921 0.2175 0.883 0.377133 x 0.9442 0.2824 3.343 0.000829 *** --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 138.47 on 99 degrees of freedom Residual deviance: 125.01 on 98 degrees of freedom AIC: 129.01 ##### #####Compute marginal effects ##### #Way 1 mean(fitted(reg))*mean(1-fitted(reg))*coefficients(reg)[2] 0.2356697 #Way 2 mean(fitted(reg)*(1-fitted(reg))*coefficients(reg)[2]) 0.2057041 ##### #####Check with Stata ##### Logistic regression Number of obs = 100 LR chi2(1) = 13.46 Prob > chi2 = 0.0002 Log likelihood = -62.506426 Pseudo R2 = 0.0972 ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .9441896 .2824403 3.34 0.001 .3906167 1.497762 _cons | .1920529 .2174531 0.88 0.377 -.2341474 .6182532 ---------------------------------------------------------------------------- -- ##### #####Compute marginal effects in Stata ##### #Way 1 Marginal effects after logit y = Pr(y) (predict) = .52354297 ---------------------------------------------------------------------------- -- variable | dy/dx Std. Err. z P>|z| [ 95% C.I. ] X ---------+------------------------------------------------------------------ -- x | .2355241 .07041 3.35 0.001 .097532 .373516 -.103593 ---------------------------------------------------------------------------- -- #Way 2 Average marginal effects on Prob(y==1) after logit ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .2057041 .0473328 4.35 0.000 .1129334 .2984747 ---------------------------------------------------------------------------- -- HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Ursprüngliche Nachricht----- Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Roberto Patuelli Gesendet: Monday, November 09, 2009 12:04 PM An: [hidden email] Betreff: [R] Percentage effects in logistic regression Dear ALL, I'm trying to figure out what the percentage effects are in a logistic regression. To be more clear, I'm not interested in the effect on y of a 1-unit increase in x, but on the percentage effect on y of a 1% increase in x (in economics this is also often called an "elasticity"). For example, if my independent variables are in logs, the betas can be directly interpreted as percentage effects both in OLS and Poisson regression. What about the logistic regression? Is there a package (maybe effects?) that can compute these automatically? Thanks and best regards, Roberto Patuelli ******************** Roberto Patuelli, Ph.D. Istituto Ricerche Economiche (IRE) (Institute for Economic Research) Università della Svizzera Italiana (University of Lugano) via Maderno 24, CP 4361 CH-6904 Lugano Switzerland Phone: +41-(0)58-666-4166 Fax: +39-02-700419665 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
||||||||||||||||
|
Roberto Patuelli-2
|
Dear Daniel,
Thanks for your prompt reply. Indeed I was aware of the possibility of computing at mean(x) or doing the mean afterwards. But what you suggest is marginal effects, right? Isn't that the effect on y of a 1-unit increase in x (what I was not interested in)? I'm interested in the effect on y of a 1% increase in x (called percentage effects, right?). Could you please clarify? Thanks Roberto ----- Original Message ----- From: "Daniel Malter" <[hidden email]> To: "Patuelli Roberto" <[hidden email]>; <[hidden email]> Sent: Monday, November 09, 2009 7:44 PM Subject: AW: [R] Percentage effects in logistic regression Somebody might have done this, but in fact it's not difficult to compute the marginal effects yourself (which is the beauty of R). For a univariate logistic regression, I illustrate two ways to compute the marginal effects (one corresponds to the mfx, the other one to the margeff command in Stata). With the first you compute the marginal effect based on the mean fitted values; with the second you compute the marginal effect based on the fitted values for each observation and then mean over the individual marginal effects. Often the second way is considered better. You can easily extend the R-code below to a multivariate regression. ##### #####Simulate data and run regression ##### set.seed(343) x=rnorm(100,0,1) #linear predictor lp=exp(x)/(1+exp(x)) #probability y=rbinom(100,1,lp) #Bernoulli draws with probability lp #Run logistic regression reg=glm(y~x,binomial) summary(reg) ##### #####Regression output ##### Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.1921 0.2175 0.883 0.377133 x 0.9442 0.2824 3.343 0.000829 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 138.47 on 99 degrees of freedom Residual deviance: 125.01 on 98 degrees of freedom AIC: 129.01 ##### #####Compute marginal effects ##### #Way 1 mean(fitted(reg))*mean(1-fitted(reg))*coefficients(reg)[2] 0.2356697 #Way 2 mean(fitted(reg)*(1-fitted(reg))*coefficients(reg)[2]) 0.2057041 ##### #####Check with Stata ##### Logistic regression Number of obs = 100 LR chi2(1) = 13.46 Prob > chi2 = 0.0002 Log likelihood = -62.506426 Pseudo R2 = 0.0972 ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .9441896 .2824403 3.34 0.001 .3906167 1.497762 _cons | .1920529 .2174531 0.88 0.377 -.2341474 .6182532 ---------------------------------------------------------------------------- -- ##### #####Compute marginal effects in Stata ##### #Way 1 Marginal effects after logit y = Pr(y) (predict) = .52354297 ---------------------------------------------------------------------------- -- variable | dy/dx Std. Err. z P>|z| [ 95% C.I. ] X ---------+------------------------------------------------------------------ -- x | .2355241 .07041 3.35 0.001 .097532 .373516 -.103593 ---------------------------------------------------------------------------- -- #Way 2 Average marginal effects on Prob(y==1) after logit ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .2057041 .0473328 4.35 0.000 .1129334 .2984747 ---------------------------------------------------------------------------- -- HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Ursprüngliche Nachricht----- Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Roberto Patuelli Gesendet: Monday, November 09, 2009 12:04 PM An: [hidden email] Betreff: [R] Percentage effects in logistic regression Dear ALL, I'm trying to figure out what the percentage effects are in a logistic regression. To be more clear, I'm not interested in the effect on y of a 1-unit increase in x, but on the percentage effect on y of a 1% increase in x (in economics this is also often called an "elasticity"). For example, if my independent variables are in logs, the betas can be directly interpreted as percentage effects both in OLS and Poisson regression. What about the logistic regression? Is there a package (maybe effects?) that can compute these automatically? Thanks and best regards, Roberto Patuelli ******************** Roberto Patuelli, Ph.D. Istituto Ricerche Economiche (IRE) (Institute for Economic Research) Università della Svizzera Italiana (University of Lugano) via Maderno 24, CP 4361 CH-6904 Lugano Switzerland Phone: +41-(0)58-666-4166 Fax: +39-02-700419665 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
||||||||||||||||
|
David Winsemius
|
On Nov 9, 2009, at 1:54 PM, Roberto Patuelli wrote: > Dear Daniel, > > Thanks for your prompt reply. > Indeed I was aware of the possibility of computing at mean(x) or > doing the mean afterwards. > But what you suggest is marginal effects, right? They might be called "marginal effects" by some. > Isn't that the effect on y of a 1-unit increase in x (what I was not > interested in)? Not exactly. The coefficient in a logistic regression analysis is the increase in log-odds(y) for a one unit increase in x. On the original y scale, the odds ratio for event y=1 (versus event y=0) for two situations differing by one unit of x would be exp(coef(x)). I'm concerned that this distinction may have escaped you since you stated that Poisson regression coefficients would have the same interpretation as OLS estimates. > I'm interested in the effect on y of a 1% increase in x (called > percentage effects, right?). You might attract more interest if you posed a specific question regarding a specific dataset which you analyzed using methods which you may understand. The term "percentage effect" may be a domain- specific term for something that has a particular interpretation, but it's not a familiar term for some of us readers. > > Could you please clarify? > > Thanks > Roberto > > > ----- Original Message ----- From: "Daniel Malter" <[hidden email]> > To: "Patuelli Roberto" <[hidden email]>; <[hidden email] > > > Sent: Monday, November 09, 2009 7:44 PM > Subject: AW: [R] Percentage effects in logistic regression > > > Somebody might have done this, but in fact it's not difficult to > compute the > marginal effects yourself (which is the beauty of R). For a univariate > logistic regression, I illustrate two ways to compute the marginal > effects > (one corresponds to the mfx, the other one to the margeff command in > Stata). > With the first you compute the marginal effect based on the mean > fitted > values; with the second you compute the marginal effect based on the > fitted > values for each observation and then mean over the individual marginal > effects. Often the second way is considered better. You can easily > extend > the R-code below to a multivariate regression. > > ##### > #####Simulate data and run regression > ##### > > set.seed(343) > x=rnorm(100,0,1) #linear predictor > lp=exp(x)/(1+exp(x)) #probability > y=rbinom(100,1,lp) #Bernoulli draws with probability lp > > #Run logistic regression > reg=glm(y~x,binomial) > summary(reg) > > ##### > #####Regression output > ##### > > Coefficients: > Estimate Std. Error z value Pr(>|z|) > (Intercept) 0.1921 0.2175 0.883 0.377133 > x 0.9442 0.2824 3.343 0.000829 *** > --- > Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > > (Dispersion parameter for binomial family taken to be 1) > > Null deviance: 138.47 on 99 degrees of freedom > Residual deviance: 125.01 on 98 degrees of freedom > AIC: 129.01 > > ##### > #####Compute marginal effects > ##### > > #Way 1 > mean(fitted(reg))*mean(1-fitted(reg))*coefficients(reg)[2] > > 0.2356697 > > #Way 2 > mean(fitted(reg)*(1-fitted(reg))*coefficients(reg)[2]) > > 0.2057041 > > > ##### > #####Check with Stata > ##### > > Logistic regression Number of obs = > 100 > LR chi2(1) = > 13.46 > Prob > chi2 = > 0.0002 > Log likelihood = -62.506426 Pseudo R2 = > 0.0972 > > ---------------------------------------------------------------------------- > -- > y | Coef. Std. Err. z P>|z| [95% Conf. > Interval] > ------------- > +-------------------------------------------------------------- > -- > x | .9441896 .2824403 3.34 0.001 .3906167 > 1.497762 > _cons | .1920529 .2174531 0.88 0.377 -.2341474 > .6182532 > ---------------------------------------------------------------------------- > -- > > ##### > #####Compute marginal effects in Stata > ##### > > #Way 1 > Marginal effects after logit > y = Pr(y) (predict) > = .52354297 > ---------------------------------------------------------------------------- > -- > variable | dy/dx Std. Err. z P>|z| [ 95% > C.I. ] X > --------- > +------------------------------------------------------------------ > -- > x | .2355241 .07041 3.35 0.001 .097532 .373516 > -.103593 > ---------------------------------------------------------------------------- > -- > > #Way 2 > Average marginal effects on Prob(y==1) after logit > > ---------------------------------------------------------------------------- > -- > y | Coef. Std. Err. z P>|z| [95% Conf. > Interval] > ------------- > +-------------------------------------------------------------- > -- > x | .2057041 .0473328 4.35 0.000 .1129334 > .2984747 > ---------------------------------------------------------------------------- > -- > > > HTH, > Daniel > > > > ------------------------- > cuncta stricte discussurus > ------------------------- > > -----Ursprüngliche Nachricht----- > Von: [hidden email] [mailto:r-help-bounces@r- > project.org] Im > Auftrag von Roberto Patuelli > Gesendet: Monday, November 09, 2009 12:04 PM > An: [hidden email] > Betreff: [R] Percentage effects in logistic regression > > Dear ALL, > > I'm trying to figure out what the percentage effects are in a logistic > regression. To be more clear, I'm not interested in the effect on y > of a > 1-unit increase in x, but on the percentage effect on y of a 1% > increase in > x (in economics this is also often called an "elasticity"). > For example, if my independent variables are in logs, the betas can be > directly interpreted as percentage effects both in OLS and Poisson > regression. What about the logistic regression? > > Is there a package (maybe effects?) that can compute these > automatically? > > Thanks and best regards, > Roberto Patuelli > > > > ******************** > Roberto Patuelli, Ph.D. > Istituto Ricerche Economiche (IRE) (Institute for Economic Research) > Università della Svizzera Italiana (University of Lugano) via > Maderno 24, CP > 4361 > CH-6904 Lugano > Switzerland > Phone: +41-(0)58-666-4166 > Fax: +39-02-700419665 > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD Heritage Laboratories West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
||||||||||||||||
|
Daniel Malter
|
In reply to this post
by Roberto Patuelli-2
Yes, it is the marginal effect. The marginal effect (dy/dx) is the slope of
the gradient at x. It is thus NOT for a 1 unit increase in x, but for a marginal change in x. Remember that, for nonlinear functions, the marginal effect is more accurate in predicting a change in y the smaller (!) the change of x is. Since you are interested in a 1% change, 1% is probably justifiable as being a small change. Thus, if you increase x by 1%, the change in y should be approximately 0.01*abs(x)*margeff. This assumes that the linear extrapolation done with a marginal effect is reasonably accurate for a prediction of y at x+delta(x). You could also compute the effect at a 1% increase in x directly (see code below). Predict the regression, but substitue x by z=x+0.01*abs(x). This gives you the predicted odds (predict.1percent) at z (which is one percent greater than x). From the odds, you can easily compute the probabilities (probs). Then subtract the fitted probabilities at x from the predicted probabilities at z, which gives you the difference in probability. In the example I sent you, this gives a change in probability of 0.001255946. This is already much smaller than the marginal effect that would be estimated at around 0.002 for a 1 percent change in x (0.2*0.01=0.002), which already indicates the declining accuracy of the marginal effect as the distance from x increases. z=x+0.01*abs(x) predict.1percent=predict(reg,list(x=z)) probs=exp(predict.1percent)/(1+exp(predict.1percent)) mean(probs-fitted(reg)) HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Ursprüngliche Nachricht----- Von: Roberto Patuelli [mailto:[hidden email]] Gesendet: Monday, November 09, 2009 1:54 PM An: Daniel Malter; [hidden email] Betreff: Re: [R] Percentage effects in logistic regression Dear Daniel, Thanks for your prompt reply. Indeed I was aware of the possibility of computing at mean(x) or doing the mean afterwards. But what you suggest is marginal effects, right? Isn't that the effect on y of a 1-unit increase in x (what I was not interested in)? I'm interested in the effect on y of a 1% increase in x (called percentage effects, right?). Could you please clarify? Thanks Roberto ----- Original Message ----- From: "Daniel Malter" <[hidden email]> To: "Patuelli Roberto" <[hidden email]>; <[hidden email]> Sent: Monday, November 09, 2009 7:44 PM Subject: AW: [R] Percentage effects in logistic regression Somebody might have done this, but in fact it's not difficult to compute the marginal effects yourself (which is the beauty of R). For a univariate logistic regression, I illustrate two ways to compute the marginal effects (one corresponds to the mfx, the other one to the margeff command in Stata). With the first you compute the marginal effect based on the mean fitted values; with the second you compute the marginal effect based on the fitted values for each observation and then mean over the individual marginal effects. Often the second way is considered better. You can easily extend the R-code below to a multivariate regression. ##### #####Simulate data and run regression ##### set.seed(343) x=rnorm(100,0,1) #linear predictor lp=exp(x)/(1+exp(x)) #probability y=rbinom(100,1,lp) #Bernoulli draws with probability lp #Run logistic regression reg=glm(y~x,binomial) summary(reg) ##### #####Regression output ##### Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.1921 0.2175 0.883 0.377133 x 0.9442 0.2824 3.343 0.000829 *** --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 138.47 on 99 degrees of freedom Residual deviance: 125.01 on 98 degrees of freedom AIC: 129.01 ##### #####Compute marginal effects ##### #Way 1 mean(fitted(reg))*mean(1-fitted(reg))*coefficients(reg)[2] 0.2356697 #Way 2 mean(fitted(reg)*(1-fitted(reg))*coefficients(reg)[2]) 0.2057041 ##### #####Check with Stata ##### Logistic regression Number of obs = 100 LR chi2(1) = 13.46 Prob > chi2 = 0.0002 Log likelihood = -62.506426 Pseudo R2 = 0.0972 ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .9441896 .2824403 3.34 0.001 .3906167 1.497762 _cons | .1920529 .2174531 0.88 0.377 -.2341474 .6182532 ---------------------------------------------------------------------------- -- ##### #####Compute marginal effects in Stata ##### #Way 1 Marginal effects after logit y = Pr(y) (predict) = .52354297 ---------------------------------------------------------------------------- -- variable | dy/dx Std. Err. z P>|z| [ 95% C.I. ] X ---------+------------------------------------------------------------------ -- x | .2355241 .07041 3.35 0.001 .097532 .373516 -.103593 ---------------------------------------------------------------------------- -- #Way 2 Average marginal effects on Prob(y==1) after logit ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .2057041 .0473328 4.35 0.000 .1129334 .2984747 ---------------------------------------------------------------------------- -- HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Ursprüngliche Nachricht----- Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Roberto Patuelli Gesendet: Monday, November 09, 2009 12:04 PM An: [hidden email] Betreff: [R] Percentage effects in logistic regression Dear ALL, I'm trying to figure out what the percentage effects are in a logistic regression. To be more clear, I'm not interested in the effect on y of a 1-unit increase in x, but on the percentage effect on y of a 1% increase in x (in economics this is also often called an "elasticity"). For example, if my independent variables are in logs, the betas can be directly interpreted as percentage effects both in OLS and Poisson regression. What about the logistic regression? Is there a package (maybe effects?) that can compute these automatically? Thanks and best regards, Roberto Patuelli ******************** Roberto Patuelli, Ph.D. Istituto Ricerche Economiche (IRE) (Institute for Economic Research) Università della Svizzera Italiana (University of Lugano) via Maderno 24, CP 4361 CH-6904 Lugano Switzerland Phone: +41-(0)58-666-4166 Fax: +39-02-700419665 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
||||||||||||||||
|
Daniel Malter
|
I should heed my own words: the 1% effect based on the marginal effect would be
0.01* ABS(X) * margeff I omitted the abs(x) in the last paragraph of my last email. Based on the marginal effect, the expected change in probability would be 0.01*0.69*0.02, which is 0.00138. This is not all too far away from the 0.00126 change based on direct predictions of the regression model that assume a one-percent increase in x for each x. Daniel
|
||||||||||||||||
|
Roberto Patuelli-2
|
In reply to this post
by Daniel Malter
Dear Daniel,
Thanks for your reply. Elasticity (what I am looking for) is defined as: dln(x)/dln(y) = dx/dy * y/x (in words, the derivative of ln(x) in ln(y), which is equal to the derivative of x in y, times the ratio between y and x) (http://en.wikipedia.org/wiki/Elasticity_(economics)). I think the same concept is used in other fields (such as medicine) by the name "percentage effects". It should not be difficult to find but somehow I'm having problems in solving this small issue... Anyone has used this in R before? Thanks Roberto ----- Original Message ----- From: "Daniel Malter" <[hidden email]> To: "Patuelli Roberto" <[hidden email]>; <[hidden email]> Sent: Monday, November 09, 2009 8:58 PM Subject: AW: [R] Percentage effects in logistic regression Yes, it is the marginal effect. The marginal effect (dy/dx) is the slope of the gradient at x. It is thus NOT for a 1 unit increase in x, but for a marginal change in x. Remember that, for nonlinear functions, the marginal effect is more accurate in predicting a change in y the smaller (!) the change of x is. Since you are interested in a 1% change, 1% is probably justifiable as being a small change. Thus, if you increase x by 1%, the change in y should be approximately 0.01*abs(x)*margeff. This assumes that the linear extrapolation done with a marginal effect is reasonably accurate for a prediction of y at x+delta(x). You could also compute the effect at a 1% increase in x directly (see code below). Predict the regression, but substitue x by z=x+0.01*abs(x). This gives you the predicted odds (predict.1percent) at z (which is one percent greater than x). From the odds, you can easily compute the probabilities (probs). Then subtract the fitted probabilities at x from the predicted probabilities at z, which gives you the difference in probability. In the example I sent you, this gives a change in probability of 0.001255946. This is already much smaller than the marginal effect that would be estimated at around 0.002 for a 1 percent change in x (0.2*0.01=0.002), which already indicates the declining accuracy of the marginal effect as the distance from x increases. z=x+0.01*abs(x) predict.1percent=predict(reg,list(x=z)) probs=exp(predict.1percent)/(1+exp(predict.1percent)) mean(probs-fitted(reg)) HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Ursprüngliche Nachricht----- Von: Roberto Patuelli [mailto:[hidden email]] Gesendet: Monday, November 09, 2009 1:54 PM An: Daniel Malter; [hidden email] Betreff: Re: [R] Percentage effects in logistic regression Dear Daniel, Thanks for your prompt reply. Indeed I was aware of the possibility of computing at mean(x) or doing the mean afterwards. But what you suggest is marginal effects, right? Isn't that the effect on y of a 1-unit increase in x (what I was not interested in)? I'm interested in the effect on y of a 1% increase in x (called percentage effects, right?). Could you please clarify? Thanks Roberto ----- Original Message ----- From: "Daniel Malter" <[hidden email]> To: "Patuelli Roberto" <[hidden email]>; <[hidden email]> Sent: Monday, November 09, 2009 7:44 PM Subject: AW: [R] Percentage effects in logistic regression Somebody might have done this, but in fact it's not difficult to compute the marginal effects yourself (which is the beauty of R). For a univariate logistic regression, I illustrate two ways to compute the marginal effects (one corresponds to the mfx, the other one to the margeff command in Stata). With the first you compute the marginal effect based on the mean fitted values; with the second you compute the marginal effect based on the fitted values for each observation and then mean over the individual marginal effects. Often the second way is considered better. You can easily extend the R-code below to a multivariate regression. ##### #####Simulate data and run regression ##### set.seed(343) x=rnorm(100,0,1) #linear predictor lp=exp(x)/(1+exp(x)) #probability y=rbinom(100,1,lp) #Bernoulli draws with probability lp #Run logistic regression reg=glm(y~x,binomial) summary(reg) ##### #####Regression output ##### Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.1921 0.2175 0.883 0.377133 x 0.9442 0.2824 3.343 0.000829 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 138.47 on 99 degrees of freedom Residual deviance: 125.01 on 98 degrees of freedom AIC: 129.01 ##### #####Compute marginal effects ##### #Way 1 mean(fitted(reg))*mean(1-fitted(reg))*coefficients(reg)[2] 0.2356697 #Way 2 mean(fitted(reg)*(1-fitted(reg))*coefficients(reg)[2]) 0.2057041 ##### #####Check with Stata ##### Logistic regression Number of obs = 100 LR chi2(1) = 13.46 Prob > chi2 = 0.0002 Log likelihood = -62.506426 Pseudo R2 = 0.0972 ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .9441896 .2824403 3.34 0.001 .3906167 1.497762 _cons | .1920529 .2174531 0.88 0.377 -.2341474 .6182532 ---------------------------------------------------------------------------- -- ##### #####Compute marginal effects in Stata ##### #Way 1 Marginal effects after logit y = Pr(y) (predict) = .52354297 ---------------------------------------------------------------------------- -- variable | dy/dx Std. Err. z P>|z| [ 95% C.I. ] X ---------+------------------------------------------------------------------ -- x | .2355241 .07041 3.35 0.001 .097532 .373516 -.103593 ---------------------------------------------------------------------------- -- #Way 2 Average marginal effects on Prob(y==1) after logit ---------------------------------------------------------------------------- -- y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+-------------------------------------------------------------- -- x | .2057041 .0473328 4.35 0.000 .1129334 .2984747 ---------------------------------------------------------------------------- -- HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Ursprüngliche Nachricht----- Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Roberto Patuelli Gesendet: Monday, November 09, 2009 12:04 PM An: [hidden email] Betreff: [R] Percentage effects in logistic regression Dear ALL, I'm trying to figure out what the percentage effects are in a logistic regression. To be more clear, I'm not interested in the effect on y of a 1-unit increase in x, but on the percentage effect on y of a 1% increase in x (in economics this is also often called an "elasticity"). For example, if my independent variables are in logs, the betas can be directly interpreted as percentage effects both in OLS and Poisson regression. What about the logistic regression? Is there a package (maybe effects?) that can compute these automatically? Thanks and best regards, Roberto Patuelli ******************** Roberto Patuelli, Ph.D. Istituto Ricerche Economiche (IRE) (Institute for Economic Research) Università della Svizzera Italiana (University of Lugano) via Maderno 24, CP 4361 CH-6904 Lugano Switzerland Phone: +41-(0)58-666-4166 Fax: +39-02-700419665 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |