-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedLinearMixedModel.Rmd
156 lines (104 loc) · 3.25 KB
/
WeightedLinearMixedModel.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
title: "LinearMixedModelPropensity"
author: "Christina De Cesaris"
date: "3/10/2021"
output: html_document
---
```{r}
library(lmerTest)
library(MASS)
library(AER)
library(scales)
library(redres)
library(MatchIt)
library(stargazer)
#install.packages("stargazer")
data(Guns)
```
```{r transformation}
#distribution of response
par(mfrow=c(1,2))
hist(Guns$violent) #TODO, ADD AXIS
hist(log(Guns$violent))
```
```{r levels plot}
#quick inverse weight propensity scores for law
sapply(Guns, class)
cor(Filter(is.numeric,Guns))
pairs(Filter(is.numeric,Guns)) #correlations
boxplot(Guns$violent~Guns$law) #TODO, add axis, legend, titles etc
```
```{r Unweighted LMM}
#before weights
mod_pre = lmer(log(violent)~law+male+robbery+murder+prisoners+density+(1|state)+(1|year), data=Guns)
summary(mod_pre)
```
```{r prebalance, results='asis' }
library(knitr)
#wip balance analysis
#bal1= glm(violent~l, Guns)
#summary(bal1)
bal1= lm(male~law, Guns)
kpre1=kable(summary(bal1)[4],
caption = "lm(male ~ law)")
bal2= lm(robbery~law, Guns)
kpre2=kable(summary(bal2)[4],
caption = "lm(robbery ~ law)")
bal3= lm(murder~law, Guns)
kpre3=kable(summary(bal3)[4],
caption = "lm(murder ~ law)")
bal4= lm(density~law, Guns)
kpre4=kable(summary(bal4)[4],
caption = "lm(density ~ law)")
bal_pre = list(kpre1,kpre2, kpre3, kpre4)
bal_pre
```
```{r pscores}
#obtain weights using logistic regression and inverse weighted propensity scores
mod_log = glm(law~male+robbery+murder+prisoners+density,family=binomial, Guns)
summary(mod_log)
prob= mod_log$fitted
pscore = ifelse(Guns$law=='yes', prob, 1-prob)
Guns$pscore=pscore
weight=1/pscore
hist(pscore) #distribution right skewed, not terrible extremes, consider trimming [0.01,0.99]
summary(pscore)
Guns$pscore=pscore
Guns$pscore=ifelse(pscore>0.99, 0.99, Guns$pscore)
Guns$pscore=ifelse(pscore<0.01, 0.01, Guns$pscore)
summary(Guns$pscore)
col.alpha<-function(color, alpha){
code=col2rgb(color)/256
rgb(code[1],code[2],code[3],alpha)
}
#better hist
hist(Guns$pscore[Guns$law=='yes'], breaks=25, col=col.alpha("red",.6), freq=F, ylim=c(0,5), xlab="Propensity Score", ylab="", main="Propensity Score Distribution")
hist((Guns$pscore[Guns$law=='no']), breaks=25, col=col.alpha("lightblue",.6), freq=F, ylim=c(0,5), xlab="Propensity Score", ylab="", main="",add=T)
legend(.4, 5, legend=c("Law: Yes", "Law: No"),
col=c("red", "blue"),fill = c("red", "lightblue") )
```
```{r postbalance,results='asis' }
bal1= lm(male~law, Guns, weights=weight)
kpost1=kable(summary(bal1)[5],
caption = "lm(male ~ law)")
bal2= lm(robbery~law, Guns, weights=weight)
kpost2=kable(summary(bal2)[5],
caption = "lm(robbery ~ law)")
bal3= lm(murder~law, Guns, weights=weight)
kpost3=kable(summary(bal3)[5],
caption = "lm(murder ~ law)")
bal4= lm(density~law, Guns, weights=weight)
kpost4=kable(summary(bal4)[5],
caption = "lm(density ~ law)")
bal_post = list(kpost1,kpost2, kpost3, kpost4)
bal_post
```
```{r weighted LMM}
mod_post = lmer(log(violent)~law+male+robbery+murder+prisoners+density+(1|state)+(1|year), data=Guns, weights=weight)
summary(mod_post)
```
```{r Diagnostics}
plot_redres(mod_post, type = "std_cond")
plot_resqq(mod_post)
plot_ranef(mod_post)
```