-
Notifications
You must be signed in to change notification settings - Fork 3
/
quantile_regression.Rmd
187 lines (145 loc) · 5.63 KB
/
quantile_regression.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
---
title: "Quantile Regression"
output:
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source("install_packages.r")
require(foreign)
require(knitr)
require(quantreg)
require(ggplot2)
require(gridExtra)
require(RColorBrewer)
```
### Introduction
This example is from MICROECONOMETRICS USING STATA
(A. C. Cameron and P. K. Trivedi)
R package required for quantile regression is `{guantreg}`
### Loading data
To load data in Stata format, we can use `read.dta` from `{foreign}`
```{r load_data}
dta <- read.dta("dta/mus03data.dta")
apply(dta, 2,function(x) sum(is.na(x))) # number of NA in each column
```
```{r}
dta <- dta[,c("ltotexp", "suppins","totchr", "age", "female", "white")]
dta <- na.omit(dta)
str(dta)
```
| Variable | Description | Type |
|----------|------------------------------|---------|
| ltotexp | log of medical expenditure | num |
| suppins | supplement private insurance | binary |
| totchr | # of chronic problems (0-7) | num |
| age | age (sample: 65-90) | integer |
| female | gender dummy | binary |
| white | race dummy | binary |
### Descriptive statistics
```{r}
summary(dta)
```
### Descriptive plots
```{r, echo=FALSE}
ggplot(dta) +
geom_boxplot(aes(as.factor(age), ltotexp)) +
facet_grid(~white) +
xlab("Age, White = 1") +
ylab("log of medical expenditure") +
ggtitle("Boxplot of ltotexp by age and race")
#--------------------------------
ggplot(dta) +
geom_boxplot(aes(as.factor(age), ltotexp)) +
facet_grid(~female) + xlab("Age, Female = 1") +
ylab("log of medical expenditure") +
ggtitle("Boxplot of ltotexp by age and gender")
#--------------------------------
ggplot(dta) +
geom_boxplot(aes(as.factor(suppins), ltotexp)) +
facet_grid(~white) + xlab("Supplement private insurance") +
ylab("log of medical expenditure") +
ggtitle("Boxplot of ltotexp by supplement private insurance and race")
#--------------------------------
ggplot(dta) +
geom_boxplot(aes(as.factor(totchr), ltotexp)) +
facet_grid(~white) + xlab("Health status") +
ylab("log of medical expenditure") +
ggtitle("Boxplot of ltotexp by health status and race")
#--------------------------------
colourCount = length(unique(dta$age))
getPalette = colorRampPalette(brewer.pal(9, "YlGnBu"))
ggplot(dta) + geom_histogram(aes(totchr, fill=as.factor(age), group= age), bins=7) +
scale_fill_manual(values = getPalette(colourCount))+ggtitle("Histogram: Count of of chronic problems by age")+guides(fill=guide_legend(title="Age"))
```
```{r , echo=FALSE}
(quantile(dta$ltotexp, c(0.1, 0.5, 0.9)))
plot(seq(0,1, by = (1/(2955-1))),dta$ltotexp, type="s", main = "ln(totexp)",xlab="fraction of the data", ylab="quantiles of ln(totexp) if totexp >0")
abline(v=c(0.1, 0.5, 0.9), col="red")
```
#OLS regression
```{r}
summary(lm.model <- lm(ltotexp~suppins+age+female+white+totchr, data=dta))
```
```{r, echo=FALSE}
resid <- residuals(lm.model)
fitted <- fitted(lm.model)
grid.arrange(
ggplot()+geom_point(aes(fitted,resid))+geom_hline(yintercept=mean(resid), colour="red"),
ggplot()+geom_boxplot(aes(as.factor(dta$age),resid))+geom_hline(yintercept=mean(resid), colour="red"),
ggplot()+geom_boxplot(aes(as.factor(dta$totchr),resid))+geom_hline(yintercept=mean(resid), colour="red"),
ggplot()+geom_line(aes(1:length(dta$ltotexp),dta$ltotexp))+geom_line(aes(1:length(dta$ltotexp),fitted, colour="red")),
ncol = 2, nrow = 2)
```
### Median regression
```{r}
summary(qr.model.5 <- rq(ltotexp~., data=dta, tau=0.5))
```
```{r}
#effects(qr.model.5)
```
### Comparing Qunatile Regression and OLS
```{r , echo=FALSE}
df.compare <- data.frame(OLS.coef = summary(lm.model)$coefficients[,1], OLS.tval= summary(lm.model)$coefficients[,3])
df.compare.coef <- data.frame(OLS.coef = summary(lm.model)$coefficients[,1])
df.compare.tval <- data.frame(OLS.tval= summary(lm.model)$coefficients[,3])
for(q in c(0.10, 0.25, 0.50, 0.75, 0.90)){
model <- rq(ltotexp~., data=dta, tau=q)
assign(paste0("qr.model",q), model)
df.compare<-cbind(df.compare, summary(model)$coefficients[,1], summary(model)$coefficients[,3])
df.compare.coef<-cbind(df.compare.coef, summary(model)$coefficients[,1])
df.compare.tval<-cbind(df.compare.tval,summary(model)$coefficients[,3])
}
colnames(df.compare) <- c("OLS.coef","OLS.tval", "QR.10.coef", "QR.10.tval", "QR.25.coef", "QR.25.tval", "QR.50.coef", "QR.50.tval", "QR.75.coef", "QR.75.tval", "QR.90.coef", "QR.90.tval")
colnames(df.compare.coef) <- c("OLS.coef", "QR.10.coef", "QR.25.coef", "QR.50.coef", "QR.75.coef", "QR.90.coef")
colnames(df.compare.tval) <- c("OLS.tval", "QR.10.tval", "QR.25.tval", "QR.50.tval", "QR.75.tval", "QR.90.tval")
```
```{r, echo=FALSE}
kable(df.compare)
kable(df.compare.coef)
kable(df.compare.tval)
#df.compare
#df.compare.coef
#df.compare.tval
```
```{r}
anova(qr.model0.25, qr.model0.5)
anova(qr.model0.5, qr.model0.75)
anova(qr.model0.25,qr.model0.75)
```
$H_0$ no difference in coefficients
```{r, echo=FALSE}
plot(qr.model<-rq(ltotexp~., data=dta, tau=seq(.1,0.9,0.1)), mar = c(5.1, 4.1, 2.1, 2.1), xlab="Quantile", mfrow=c(2,3) )
```
```{r, echo=FALSE}
qr.model_plot<-rq(ltotexp~totchr, data=dta, tau=c(0.1,0.25,0.5,0.75,0.09))
resid <- residuals(qr.model_plot)
fitted <- fitted(qr.model_plot)
ggplot()+
geom_line(aes(dta$totchr,fitted[,1], color=paste("tau 0.1")))+
geom_line(aes(dta$totchr,fitted[,2], color=paste("tau 0.25")))+
geom_line(aes(dta$totchr,fitted[,3], color=paste("tau 0.5")))+
geom_line(aes(dta$totchr,fitted[,4], color=paste("tau 0.75")))+
geom_line(aes(dta$totchr,fitted[,5], color=paste("tau 0.90")))+
xlab("totchr")+ylab("fitted values")
```