Part 1
library(dplyr)
library(tidyr)
library(MASS)
library(brant)
library(knitr)
set.seed(1337)
B1 <- abs(rnorm(1000))
B2 <- abs(rnorm(1000))
lambda <- exp(B1 + B2)
depVari <- rpois(1000, lambda)
poisModel <- glm(depVari ~ B1 + B2, family = "poisson")
summary(poisModel)
Call:
glm(formula = depVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-3.3948 -0.7742 -0.0681 0.6183 3.0290
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.01328 0.03056 0.435 0.664
B1 0.98458 0.01443 68.239 <2e-16 ***
B2 0.99021 0.01465 67.585 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 8500.5 on 999 degrees of freedom
Residual deviance: 1068.8 on 997 degrees of freedom
AIC: 4402.2
Number of Fisher Scoring iterations: 4
inflatedModeler <- function(x){
inflateZero <- rbinom(1000, 1, x)
zeroVari <- depVari * inflateZero
zeroModel <- glm(zeroVari ~ B1 + B2, family = "poisson")
summary(zeroModel)
}
inflatedModeler(.04)
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-4.2584 -0.8831 -0.5779 -0.4093 14.3044
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.59948 0.14485 -24.85 <2e-16 ***
B1 1.10882 0.06363 17.43 <2e-16 ***
B2 1.38639 0.05933 23.37 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 3147.1 on 999 degrees of freedom
Residual deviance: 2467.2 on 997 degrees of freedom
AIC: 2630
Number of Fisher Scoring iterations: 7
inflatedModeler(.15)
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-5.0982 -1.5456 -1.1016 -0.8067 11.2988
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.66900 0.07558 -22.08 <2e-16 ***
B1 1.04889 0.03510 29.88 <2e-16 ***
B2 0.77776 0.03911 19.89 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 5689.9 on 999 degrees of freedom
Residual deviance: 4643.8 on 997 degrees of freedom
AIC: 5193.8
Number of Fisher Scoring iterations: 7
inflatedModeler(.40)
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-7.293 -2.110 -1.475 1.267 8.645
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.60968 0.04716 -12.93 <2e-16 ***
B1 0.88229 0.02340 37.71 <2e-16 ***
B2 0.81895 0.02444 33.51 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 8219.7 on 999 degrees of freedom
Residual deviance: 6137.7 on 997 degrees of freedom
AIC: 7491.4
Number of Fisher Scoring iterations: 6
inflatedModeler(.80)
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-11.3418 -1.4076 0.0742 0.9557 3.7766
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.18490 0.03374 -5.479 4.27e-08 ***
B1 0.94621 0.01614 58.610 < 2e-16 ***
B2 1.02714 0.01601 64.170 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 9493.1 on 999 degrees of freedom
Residual deviance: 3406.0 on 997 degrees of freedom
AIC: 6115.7
Number of Fisher Scoring iterations: 5
As zero inflation gets worse, the data becomes less accurate and the standard errors increase. Most distinctly is the intercept. The intercept decreases significantly.
inflatedModeler <- function(x){
inflateZero <- rbinom(1000, 1, x)
zeroVari <- B1 * inflateZero
zeroModel <- glm(zeroVari ~ B1 + B2, family = "poisson")
summary(zeroModel)
}
suppressWarnings({inflatedModeler(.04)})
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-0.7501 -0.2702 -0.2153 -0.1797 3.1275
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.5538 0.4178 -10.900 < 2e-16 ***
B1 0.9534 0.2043 4.666 3.07e-06 ***
B2 0.2005 0.2743 0.731 0.465
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 234.61 on 999 degrees of freedom
Residual deviance: 216.01 on 997 degrees of freedom
AIC: Inf
Number of Fisher Scoring iterations: 6
suppressWarnings({inflatedModeler(.15)})
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-1.3063 -0.5228 -0.4322 -0.3685 2.1992
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.7887198 0.2018351 -13.817 < 2e-16 ***
B1 0.7631661 0.1075629 7.095 1.29e-12 ***
B2 0.0008938 0.1465193 0.006 0.995
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 538.56 on 999 degrees of freedom
Residual deviance: 495.38 on 997 degrees of freedom
AIC: Inf
Number of Fisher Scoring iterations: 6
suppressWarnings({inflatedModeler(.40)})
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-2.1716 -0.7359 -0.5617 0.5267 1.2716
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.07671 0.13428 -15.466 <2e-16 ***
B1 0.85153 0.06876 12.384 <2e-16 ***
B2 0.03562 0.09513 0.374 0.708
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 751.51 on 999 degrees of freedom
Residual deviance: 621.45 on 997 degrees of freedom
AIC: Inf
Number of Fisher Scoring iterations: 6
suppressWarnings({inflatedModeler(.80)})
Call:
glm(formula = zeroVari ~ B1 + B2, family = "poisson")
Deviance Residuals:
Min 1Q Median 3Q Max
-2.9255 -0.4148 0.1147 0.3744 0.4715
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.34701 0.09197 -14.647 <2e-16 ***
B1 0.89733 0.04615 19.443 <2e-16 ***
B2 0.01256 0.06531 0.192 0.848
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 660.01 on 999 degrees of freedom
Residual deviance: 340.55 on 997 degrees of freedom
AIC: Inf
Number of Fisher Scoring iterations: 5
Standard errors are significantly worse when the Beta is inflated. Likewise, the beta estimates are inaccurate.
Part 2
data <- read.csv("https://raw.githubusercontent.com/PrisonRodeo/PLSC504-2023-git/main/Exercises/PLSC504-2023-ExerciseTwo.csv")
model1 <- polr(data = data, formula = factor(mag010) ~ avmdiala + genyr + regtype + marg + coldwar, Hess = TRUE)
summary(model1)
Call:
polr(formula = factor(mag010) ~ avmdiala + genyr + regtype +
marg + coldwar, data = data, Hess = TRUE)
Coefficients:
Value Std. Error t value
avmdiala -0.2483561 0.09677 -2.5665
genyr -0.0377515 0.02645 -1.4273
regtype -0.0372247 0.02898 -1.2845
marg -0.0009991 0.00117 -0.8539
coldwar -0.6906209 0.30302 -2.2791
Intercepts:
Value Std. Error t value
0|1 -2.4862 0.4187 -5.9385
1|2 -1.8201 0.4023 -4.5238
2|3 -1.3918 0.3955 -3.5190
3|4 -1.0880 0.3911 -2.7817
4|5 -0.8775 0.3878 -2.2627
5|6 -0.4145 0.3816 -1.0863
6|7 0.2583 0.3813 0.6776
7|8 1.1038 0.3978 2.7748
8|9 2.1559 0.4577 4.7106
9|10 3.5032 0.6708 5.2223
Residual Deviance: 904.5161
AIC: 934.5161
(108 observations deleted due to missingness)
brant(model1)
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
--------------------------------------------
Test for X2 df probability
--------------------------------------------
Omnibus 134.44 45 0
avmdiala -20.13 9 1
genyr 11.66 9 0.23
regtype 104.69 9 0
marg -31.12 9 1
coldwar 25.91 9 0
--------------------------------------------
H0: Parallel Regression Assumption holds
In this model, the parallel regression assumption holds according to the Brant test. This suggests that the relationship between the independent variables and the dependent variable is the same across all levels of the dependent variable.
data$newVar <- data$severity
data$newVar[which(data$newVar < 1000)] <- 0
data$newVar[which(data$newVar >= 1000 & data$newVar < 8000)] <- 1
data$newVar[which(data$newVar >= 8000 & data$newVar < 64000)] <- 2
data$newVar[which(data$newVar >= 64000)] <- 3
model2 <- polr(data = data, formula = factor(newVar) ~ avmdiala + genyr + regtype + marg + coldwar, Hess = TRUE)
summary(model2)
Call:
polr(formula = factor(newVar) ~ avmdiala + genyr + regtype +
marg + coldwar, data = data, Hess = TRUE)
Coefficients:
Value Std. Error t value
avmdiala -0.2120168 0.095295 -2.2248
genyr -0.0544898 0.027479 -1.9830
regtype -0.0585291 0.030553 -1.9157
marg -0.0006255 0.001185 -0.5277
coldwar -0.8599896 0.315062 -2.7296
Intercepts:
Value Std. Error t value
0|1 -1.9206 0.4156 -4.6212
1|2 -0.9753 0.3998 -2.4394
2|3 1.0257 0.4026 2.5480
Residual Deviance: 519.8185
AIC: 535.8185
(108 observations deleted due to missingness)
brant(model2)
--------------------------------------------
Test for X2 df probability
--------------------------------------------
Omnibus 22.33 10 0.01
avmdiala 2.39 2 0.3
genyr 1.14 2 0.56
regtype 4.46 2 0.11
marg 2.52 2 0.28
coldwar 8.63 2 0.01
--------------------------------------------
H0: Parallel Regression Assumption holds
When re-aggregating values to a smaller ordinal range, the values reported by the model are less accurate and the standard errors remain close to the same. The parallel regression assumption still holds according to the Brant test– but the probability values change and for the most part, get closer to zero. This suggests that the relationship between the independent variables and the dependent variable is the same across all levels of the dependent variable.
model3 <- lm(data = data, formula = mag010 ~ avmdiala + genyr + regtype + marg + coldwar)
summary(model3)
Call:
lm(formula = mag010 ~ avmdiala + genyr + regtype + marg + coldwar,
data = data)
Residuals:
Min 1Q Median 3Q Max
-5.1912 -2.6414 0.4933 2.3436 6.5685
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.504477 0.613333 8.975 2.32e-16 ***
avmdiala -0.380845 0.149604 -2.546 0.0117 *
genyr -0.073143 0.043393 -1.686 0.0935 .
regtype -0.063954 0.047427 -1.348 0.1791
marg -0.001931 0.001942 -0.995 0.3212
coldwar -1.118382 0.481609 -2.322 0.0213 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.905 on 196 degrees of freedom
(108 observations deleted due to missingness)
Multiple R-squared: 0.05892, Adjusted R-squared: 0.03491
F-statistic: 2.454 on 5 and 196 DF, p-value: 0.03488
Using OLS, the estimates are significantly different (almost 2x more) and the standard errors are extremely large in comparison to both models run in (1) and (3). As a result, adopting an ordered-response model is very important in finding the most accurate estimates.