library(stargazer)
library(plyr)
library(VGAM)
library(mlogit)
library(plm)
library(tidyr)
library(nnet)
library(MASS)
library(lmtest)
set.seed(1337)
Part 1
N = 500
D = sample(x = c(0, 1), N, replace = TRUE)
Y = sample(x = c(0, 1), N, replace = TRUE)
B0 <- 23
B1 <- 46
logiPred <- exp(B0 + (B1 * D)) / 1 + exp(B0 + (B1 * D))
logiPred <- as.data.frame(logiPred)
myLogit <- glm(Y ~ D, data = logiPred, family = "binomial")
summary(myLogit)
Call:
glm(formula = Y ~ D, family = "binomial", data = logiPred)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.252 -1.136 1.105 1.105 1.219
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.1737 0.1260 1.379 0.168
D -0.2713 0.1793 -1.513 0.130
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 692.95 on 499 degrees of freedom
Residual deviance: 690.65 on 498 degrees of freedom
AIC: 694.65
Number of Fisher Scoring iterations: 3
#stargazer(myLogit, type = "html", title = "Logistic Regression", out = "assign1Logi.htm")
myProbit <- glm(Y ~ D, data = logiPred, family = binomial(link = "probit"))
summary(myProbit)
Call:
glm(formula = Y ~ D, family = binomial(link = "probit"), data = logiPred)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.252 -1.136 1.105 1.105 1.219
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.10877 0.07881 1.380 0.168
D -0.16994 0.11227 -1.514 0.130
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 692.95 on 499 degrees of freedom
Residual deviance: 690.65 on 498 degrees of freedom
AIC: 694.65
Number of Fisher Scoring iterations: 3
#stargazer(myProbit, type = "html", title = "Probit Regression", out = "assign1Probit.htm")
anova <- anova(myLogit, myProbit)
print(anova)
Analysis of Deviance Table
Model 1: Y ~ D
Model 2: Y ~ D
Resid. Df Resid. Dev Df Deviance
1 498 690.65
2 498 690.65 0 0
estimateDiffs <- function(){
myLogit <- glm(Y ~ D, data = logiPred, family = "binomial")
myProbit <- glm(Y ~ D, data = logiPred, family = binomial(link = "probit"))
anova <- anova(myLogit, myProbit)
return(anova)
}
finalEst <- rdply(100, estimateDiffs)
print(finalEst)
NA
estimateDiffs2 <- function(){
N = sample(x = c(50:500), 1, replace = TRUE)
B0 = sample(x = c(1:75), 1, replace = TRUE)
B1 = sample(x = c(1:75), 1, replace = TRUE)
D = sample(x = c(0, 1), N, replace = TRUE)
Y = sample(x = c(0, 1), N, replace = TRUE)
logiPred <- exp(B0 + (B1 * D)) / 1 + exp(B0 + (B1 * D))
logiPred <- as.data.frame(logiPred)
myLogit <- glm(Y ~ D, data = logiPred, family = "binomial")
myProbit <- glm(Y ~ D, data = logiPred, family = binomial(link = "probit"))
anova <- anova(myLogit, myProbit)
return(anova)
}
finalEst <- rdply(1000, estimateDiffs2)
print(finalEst)
NA
Part 2
data <- read.csv("https://raw.githubusercontent.com/PrisonRodeo/PLSC504-2023-git/main/Exercises/PLSC504-2023-ExerciseOne.csv")
mLogit <- vglm(data = data, cartype ~ female + kids + age, family = multinomial)
summary(mLogit)
Call:
vglm(formula = cartype ~ female + kids + age, family = multinomial,
data = data)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept):1 -2.027220 0.498082 -4.070 4.70e-05 ***
(Intercept):2 0.846347 0.312919 2.705 0.006837 **
(Intercept):3 -0.214092 0.388819 -0.551 0.581893
female:1 -0.080127 0.238376 -0.336 0.736767
female:2 0.106535 0.166553 0.640 0.522402
female:3 -1.542560 0.222176 -6.943 3.84e-12 ***
kids:1 0.306617 0.268062 1.144 0.252695
kids:2 -0.655219 0.179990 -3.640 0.000272 ***
kids:3 -0.076473 0.221599 -0.345 0.730021
age:1 0.029738 0.008525 3.489 0.000486 ***
age:2 0.011935 0.005762 2.071 0.038319 *
age:3 0.019966 0.007118 2.805 0.005030 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Names of linear predictors: log(mu[,1]/mu[,4]), log(mu[,2]/mu[,4]), log(mu[,3]/mu[,4])
Residual deviance: 2605.418 on 3528 degrees of freedom
Log-likelihood: -1302.709 on 3528 degrees of freedom
Number of Fisher scoring iterations: 5
No Hauck-Donner effect found in any of the estimates
Reference group is level 4 of the response
Interpreting this model:
Being a female as opposed to a male, changes the log odds of driving a truck by -1.542.
Having children as opposed to not having children, changes the log odds of driving a SUV by -0.655 Having children as opposed to not having children, changes the log odds of driving a truck by -0.0764
For every unit increase in age, the log odds of driving a car increase by .029 For every unit increase in age, the log odds of driving a SUV increase by .011 For every unit increase in age, the log odds of driving a truck increase by .019
mLogit2 <- vglm(data = data, cartype ~ female, family = multinomial)
newData <- data.frame(c(data$cartype))
newData2 <- data.frame(data$female)
colnames(newData) <- "cartype"
colnames(newData2) <- "female"
fullData <- cbind(newData, newData2)
predictedProbs <- predict(mLogit2, fullData)
plot(x = fullData$female , y = predictedProbs[,1],pch=19,
col=rgb(100,0,0,100,maxColorValue=255),
xlab="Sex",
ylab="Predicted Probability")
NA
NA
If you are a male, you are more likely to have a Truck or SUV than a female, as the predicted probability for men is -0.518 and for women it is -0.539
fullData <- data
fullData <- subset(fullData, cartype != "")
choice <- as.factor(fullData$cartype)
predictor <- as.data.frame(fullData[ , "age"])
fullData <- drop_na(fullData)
data2 <- mlogit.data(data = fullData, choice = 'cartype', shape = "wide")
mLogit <- mlogit(cartype~1 | age, data = data2)
summary(mLogit)
Call:
mlogit(formula = cartype ~ 1 | age, data = data2, method = "nr")
Frequencies of alternatives:choice
Car Pickup SUV
0.61281 0.20000 0.18719
nr method
4 iterations, 0h:0m:0s
g'(-H)^-1g = 1.57E-07
gradient close to zero
Coefficients :
Estimate Std. Error z-value Pr(>|z|)
(Intercept):Pickup -1.0554113 0.2550659 -4.1378 3.507e-05 ***
(Intercept):SUV -0.2845396 0.2546476 -1.1174 0.2638295
age:Pickup -0.0013237 0.0049876 -0.2654 0.7906994
age:SUV -0.0194810 0.0053520 -3.6399 0.0002727 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Log-Likelihood: -942.62
McFadden R^2: 0.0074387
Likelihood ratio test : chisq = 14.129 (p.value = 0.00085506)
mLogit2 <- mlogit(cartype~1, data = data2)
hmftest(mLogit, mLogit2)
Hausman-McFadden test
data: data2
chisq = -14.495, df = 2, p-value = 1
alternative hypothesis: IIA is rejected
#Not sure how to setup the second model with alt.subset
model <- multinom(choice ~ ., data = predictor, method = "probit")
# weights: 9 (4 variable)
initial value 1171.120700
final value 990.097773
converged
summary(model)
Call:
multinom(formula = choice ~ ., data = predictor, method = "probit")
Coefficients:
(Intercept) `fullData[, "age"]`
Pickup -1.043393 -0.001108194
SUV -0.310668 -0.019464039
Std. Errors:
(Intercept) `fullData[, "age"]`
Pickup 0.2479307 0.004863944
SUV 0.2519938 0.005315500
Residual Deviance: 1980.196
AIC: 1988.196
model <- multinom(choice ~ ., data = predictor, method = "logit")
# weights: 9 (4 variable)
initial value 1171.120700
final value 990.097773
converged
summary(model)
Call:
multinom(formula = choice ~ ., data = predictor, method = "logit")
Coefficients:
(Intercept) `fullData[, "age"]`
Pickup -1.043393 -0.001108194
SUV -0.310668 -0.019464039
Std. Errors:
(Intercept) `fullData[, "age"]`
Pickup 0.2479307 0.004863944
SUV 0.2519938 0.005315500
Residual Deviance: 1980.196
AIC: 1988.196
mLogit3 <- mlogit(cartype~1 | age, data = data2, heterosc = TRUE)
summary(mLogit3)
Call:
mlogit(formula = cartype ~ 1 | age, data = data2, heterosc = TRUE)
Frequencies of alternatives:choice
Car Pickup SUV
0.61281 0.20000 0.18719
bfgs method
3 iterations, 0h:0m:1s
g'(-H)^-1g = 0.422
last step couldn't find higher value
Coefficients :
Estimate Std. Error z-value Pr(>|z|)
(Intercept):Pickup -51.757145 250.963608 -0.2062 0.8366
(Intercept):SUV -21.204429 109.014243 -0.1945 0.8458
age:Pickup 0.093162 0.796898 0.1169 0.9069
age:SUV -0.629181 2.910007 -0.2162 0.8288
sp.Pickup 39.773510 184.113169 0.2160 0.8290
sp.SUV 39.865936 184.124152 0.2165 0.8286
Log-Likelihood: -939.19
McFadden R^2: 0.01105
Likelihood ratio test : chisq = 20.988 (p.value = 0.00031834)
The logit and probit models are identical. The HEV model has standard errors that are very extreme and estimates that are not accurate to those of the other two models.