Importing data: Simmons Store example (misDatos1)

fichero2 <- "https://raw.githubusercontent.com/NMANMA/classRoomFiles/master/Simmons.txt"
misDatos1 <- read.delim(file=fichero2,header=TRUE, sep = "\t", dec = ".")
head(misDatos1)  # first few rows
##   Customer Spending Card Coupon
## 1        1    2.291    1      0
## 2        2    3.215    1      0
## 3        3    2.135    1      0
## 4        4    3.924    0      0
## 5        5    2.528    1      0
## 6        6    2.473    0      1

Modelizing: : Simmons Store example (misDatos1)

model0 <- glm(Coupon ~ 1, family=binomial , data=misDatos1)

model1 <- glm(Coupon ~ Spending , family=binomial ,
                data=misDatos1)
est1 <- summary(model1)
est1
## 
## Call:
## glm(formula = Coupon ~ Spending, family = binomial, data = misDatos1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.5205  -0.9064  -0.7975   1.1838   1.6907  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)   
## (Intercept)  -1.4991     0.4735  -3.166  0.00155 **
## Spending      0.3218     0.1234   2.607  0.00912 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 134.60  on 99  degrees of freedom
## Residual deviance: 127.38  on 98  degrees of freedom
## AIC: 131.38
## 
## Number of Fisher Scoring iterations: 4
model2 <- glm(Coupon ~ Spending + Card , family=binomial ,
                data=misDatos1)
est2 <- summary(model2)
est2
## 
## Call:
## glm(formula = Coupon ~ Spending + Card, family = binomial, data = misDatos1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.6839  -1.0140  -0.6503   1.1216   1.8794  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -2.1464     0.5772  -3.718 0.000201 ***
## Spending      0.3416     0.1287   2.655 0.007928 ** 
## Card          1.0987     0.4447   2.471 0.013483 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 134.60  on 99  degrees of freedom
## Residual deviance: 120.97  on 97  degrees of freedom
## AIC: 126.97
## 
## Number of Fisher Scoring iterations: 4
test2<-anova(model0,model2)
test2
## Analysis of Deviance Table
## 
## Model 1: Coupon ~ 1
## Model 2: Coupon ~ Spending + Card
##   Resid. Df Resid. Dev Df Deviance
## 1        99     134.60            
## 2        97     120.97  2   13.628
??Chisquare
## starting httpd help server ... done
pchisq(q=test2$Deviance, df=test2$Df, lower.tail = FALSE)
## [1]          NA 0.001098051

Estimation of probabilities: Simmons Store example (misDatos1)

??predict.glm
nuevaObservacion <- data.frame(Spending=2.8)
pp<-predict(model1, nuevaObservacion, type="response")
pp
##         1 
## 0.3547899
nuevaObservacion0 <- data.frame(Spending=2.8, Card=0)
pp0<-predict(model2, nuevaObservacion0, type="response")
pp0
##         1 
## 0.2332998
nuevaObservacion1 <- data.frame(Spending=2.8, Card=1)
pp1<-predict(model2, nuevaObservacion1, type="response")
pp1
##         1 
## 0.4772557

Odds ratios: Simmons Store example (misDatos1)

# Calculation of odss ratios in model 2 using original definition
odds0 <- pp0/(1-pp0)
odds1 <- pp1/(1-pp1)
odssRatio <- odds1/odds0
odssRatio
##        1 
## 3.000359
# Calculation of odss ratios in model 2 using logistic model
exp(est2$coefficients[3,1])
## [1] 3.000359