User Tools

Site Tools


logistic_regression

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
logistic_regression [2019/09/18 07:53] hkimscillogistic_regression [2019/09/18 07:56] hkimscil
Line 16: Line 16:
 {{youtube>xxFYro8QuXA}} Logistic Regression Details Pt 3: R-squared and p-value {{youtube>xxFYro8QuXA}} Logistic Regression Details Pt 3: R-squared and p-value
 </WRAP> </WRAP>
 +
 +====== e.g. 1 ======
 +https://stats.idre.ucla.edu/r/dae/logit-regression/
 +<code>
 +mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
 +## view the first few rows of the data
 +head(mydata)
 +</code>
 +
 +<code>
 +  admit gre  gpa rank
 +1     0 380 3.61    3
 +2     1 660 3.67    3
 +3     1 800 4.00    1
 +4     1 640 3.19    4
 +5     0 520 2.93    4
 +6     1 760 3.00    2
 +</code>
 +
 +<code>summary(mydata)
 +</code>
 +
 +<code>
 +     admit             gre             gpa             rank      
 + Min.   :0.0000   Min.   :220.0   Min.   :2.260   Min.   :1.000  
 + 1st Qu.:0.0000   1st Qu.:520.0   1st Qu.:3.130   1st Qu.:2.000  
 + Median :0.0000   Median :580.0   Median :3.395   Median :2.000  
 + Mean   :0.3175   Mean   :587.7   Mean   :3.390   Mean   :2.485  
 + 3rd Qu.:1.0000   3rd Qu.:660.0   3rd Qu.:3.670   3rd Qu.:3.000  
 + Max.   :1.0000   Max.   :800.0   Max.   :4.000   Max.   :4.000  
 +</code>
 +
 +<code>sapply(mydata, mean)
 +sapply(mydata, sd)
 +</code>
 +
 +<code>> sapply(mydata, mean)
 +   admit      gre      gpa     rank 
 +  0.3175 587.7000   3.3899   2.4850 
 +> sapply(mydata, sd)
 +      admit         gre         gpa        rank 
 +  0.4660867 115.5165364   0.3805668   0.9444602 
 +> </code>
 +
 +<code>xtabs(~admit + rank, data = mydata)</code>
 +
 +<code>> xtabs(~admit + rank, data = mydata)
 +     rank
 +admit  1  2  3  4
 +    0 28 97 93 55
 +    1 33 54 28 12
 +</code>
 +
 +<code>mydata$rank <- factor(mydata$rank)
 +mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
 +summary(mylogit)
 +</code>
 +
 +<code>
 +Call:
 +glm(formula = admit ~ gre + gpa + rank, family = "binomial", 
 +    data = mydata)
 +
 +Deviance Residuals: 
 +    Min       1Q   Median       3Q      Max  
 +-1.6268  -0.8662  -0.6388   1.1490   2.0790  
 +
 +Coefficients:
 +             Estimate Std. Error z value Pr(>|z|)    
 +(Intercept) -3.989979   1.139951  -3.500 0.000465 ***
 +gre          0.002264   0.001094   2.070 0.038465 *  
 +gpa          0.804038   0.331819   2.423 0.015388 *  
 +rank2       -0.675443   0.316490  -2.134 0.032829 *  
 +rank3       -1.340204   0.345306  -3.881 0.000104 ***
 +rank4       -1.551464   0.417832  -3.713 0.000205 ***
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +(Dispersion parameter for binomial family taken to be 1)
 +
 +    Null deviance: 499.98  on 399  degrees of freedom
 +Residual deviance: 458.52  on 394  degrees of freedom
 +AIC: 470.52
 +
 +Number of Fisher Scoring iterations: 4
 +
 +> </code>
 +
 +<code>
 +> confint(mylogit)
 +Waiting for profiling to be done...
 +                    2.5 %       97.5 %
 +(Intercept) -6.2716202334 -1.792547080
 +gre          0.0001375921  0.004435874
 +gpa          0.1602959439  1.464142727
 +rank2       -1.3008888002 -0.056745722
 +rank3       -2.0276713127 -0.670372346
 +rank4       -2.4000265384 -0.753542605
 +
 +</code>
 +
 +<code>> ## CIs using standard errors
 +> confint.default(mylogit)
 +                    2.5 %       97.5 %
 +(Intercept) -6.2242418514 -1.755716295
 +gre          0.0001202298  0.004408622
 +gpa          0.1536836760  1.454391423
 +rank2       -1.2957512650 -0.055134591
 +rank3       -2.0169920597 -0.663415773
 +rank4       -2.3703986294 -0.732528724
 +</code>
 +
 +<code>wald.test(b = coef(mylogit), Sigma = vcov(mylogit), Terms = 4:6)</code>
 +
 +<code>l <- cbind(0, 0, 0, 1, -1, 0)
 +wald.test(b = coef(mylogit), Sigma = vcov(mylogit), L = l)
 +</code>
 +
 +{{tag> statistics r "logit statistics" "multiple regression"}}
 +
logistic_regression.txt · Last modified: 2023/12/14 07:55 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki