-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
286 lines (235 loc) · 5.25 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
---
title: "R to Stata Cheatsheet"
author: "UVA StatLab"
date: "2023-10-25"
output:
html_document:
toc: true
toc_float: true
---
------
R code followed by Stata code. Tested with R 4.3.1 and Stata 18.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
### import CSV from URL
```
URL <- 'https://raw.githubusercontent.com/clayford/dataviz_with_ggplot2/master/alb_homes.csv'
homes <- read.csv(file = URL)
```
```
import delimited "https://raw.githubusercontent.com/clayford/dataviz_with_ggplot2/master/alb_homes.csv"
```
### head
```
head(homes)
```
```
list in 1/6
```
### histogram
```
hist(homes$totalvalue)
```
```
histogram totalvalue
```
### density histogram
```
plot(density(homes$totalvalue))
```
```
kdensity totalvalue, kernel(gaussian)
kdensity totalvalue /*epanechnikov*/
```
### linear model
```
m1 <- lm(totalvalue ~ finsqft + bedroom + lotsize, data = homes)
summary(m1)
```
```
regress totalvalue finsqft bedroom lotsize
```
### extract coefficients
```
coef(m1)
```
```
matrix list e(b)
```
### residuals versus fitted values
```
plot(m1, which = 1)
```
```
predict resid, residuals
predict fitted, xb
rvfplot, addplot(lowess resid fitted, leg(off))
* list points of interest
list totalvalue fitted finsqft bedroom lotsize if resid > 2000000
```
### QQ plot of residuals
```
plot(m1, which = 2)
```
```
qnorm resid /*calculated above*/
```
### scale-location plot
```
plot(m1, which = 3)
```
```
predict resid_z, rstandard
replace resid_z = sqrt(abs(resid_z))
twoway scatter resid_z fitted ||
lowess resid_z fitted
```
### residuals vs leverage
```
plot(m1, which = 5)
```
```
predict lev, leverage
predict resid_z2, rstandard
twoway scatter resid_z2 lev ||
lowess resid_z2 lev
/*without Cooks D contours*/
```
### model with log-transformed response
```
m2 <- lm(log(totalvalue) ~ finsqft + bedroom + lotsize, data = homes)
```
```
gen log_totalvalue = log(totalvalue )
regress log_totalvalue finsqft bedroom lotsize
```
### model with categorical predictors
```
m4 <- lm(log(totalvalue) ~ fullbath + finsqft + hsdistrict,
data = homes)
```
```
encode hsdistrict, generate(hsdistrict_f)
regress log_totalvalue fullbath finsqft i.hsdistrict_f
```
### model with interactions
```
m6 <- lm(log(totalvalue) ~ fullbath + finsqft + hsdistrict +
fullbath:finsqft +
fullbath:hsdistrict +
finsqft:hsdistrict,
data = homes)
```
```
regress log_totalvalue fullbath finsqft i.hsdistrict_f
c.fullbath#c.finsqft
c.fullbath#i.hsdistrict_f
c.finsqft#i.hsdistrict_f
```
### model with non-linear effects
R using natural splines, Stata using restricted cubic splines
```
library(splines)
nlm3 <- lm(log(totalvalue) ~ ns(finsqft, 5) + lotsize + hsdistrict +
ns(finsqft, 5):hsdistrict,
data = homes)
```
```
makespline rcs finsqft, knots(7) basis(fsf) order(3) replace
regress log_totalvalue lotsize c.fsf*##hsdistrict_f
```
### partial F tests (Type II SS)
```
drop1(m6)
```
```
testparm c.finsqft#i.hsdistrict_f
testparm c.fullbath#hsdistrict_f
testparm c.fullbath#c.finsqft
```
### effect plot (continuous/categorical interaction)
```
library(ggeffects)
plot(ggpredict(m6, terms = c("fullbath[1:5]", "hsdistrict")))
```
```
margins i.hsdistrict_f, at( (median) finsqft fullbath=(1(1)5))
expression(exp(predict(xb)))
marginsplot
```
### effect plot (continuous/continuous interaction)
```
plot(ggpredict(m6, terms = c("finsqft[1000:4000 by=500]",
"fullbath[2:5]")))
```
```
margins, at(finsqft=(1000(500)4000) fullbath=(2(1)5) hsdistrict_f=1)
expression(exp(predict(xb)))
marginsplot
```
### AIC/BIC values
```
m4 <- lm(log(totalvalue) ~ finsqft + bedroom + lotsize,
data = homes)
AIC(m4); BIC(m4)
regress log_totalvalue finsqft bedroom lotsize
estimates stats
```
### AIC/BIC values for model comparison
```
m1 <- lm(totalvalue ~ finsqft + bedroom + lotsize, data = homes)
m2 <- lm(totalvalue ~ finsqft + bedroom, data = homes)
AIC(m1, m2)
BIC(m1, m2)
```
```
regress totalvalue finsqft bedroom lotsize
estimates store m1
regress totalvalue finsqft bedroom
estimates store m2
estimates table m1 m2, stats(aic bic)
```
### `for` loop
To iterate through a sequence of values:
```
for (i in 1:nrow(homes)) {
cat('High school district for home', i, 'is', homes$hsdistrict[i], '\n')
}
```
```
local N = _N
forval i = 1 / `N' {
display "High school district for home", `i', "is", hsdistrict[`i']
}
```
To iterate through a set of tokens:
```
sch <- c('esdistrict', 'msdistrict', 'hsdistrict')
for (i in sch) {
print(table(homes[[i]]))
}
```
```
local sch esdistrict msdistrict hsdistrict
foreach i in `sch' {
tab `i'
}
```
### multiple imputation
Example uses `rnes96` data from _Extending the Linear Model_ by Julian Faraway. Data available [here](https://gist.github.com/clayford/367f5c88a6185ad8f36f866f3f6d6d72).
```
library(mice)
md.pattern(rnes96)
imp <- mice(rnes96, m = 10, print = FALSE, seed = 99)
fit <- with(imp, multinom(party ~ age + education + income))
summary(pool(fit))
```
```
use rnes96.dta
mi set flong
mi misstable patterns
mi register imputed education income
mi impute chained (pmm, knn(5)) education income = party age, add(10)
mi estimate: mlogit party age i.education income
```