-
Notifications
You must be signed in to change notification settings - Fork 0
/
STAT101A_FINAL.R
210 lines (157 loc) · 5.82 KB
/
STAT101A_FINAL.R
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
#read csv
covid <- read.csv('kolko_covid_shareable_dataset.csv')
#dataset simplification
covid <- covid[, -c(6,7)]
covid <- covid[, -5]
covid <- covid[, -c(1, 2, 3)]
covid <- covid[, -c(1, 14)]
#creating categorical variable
summary(covid$hhsize)
hhsize_c <- character(nrow(covid))
for (i in 1:nrow(covid)) {
if (!is.na(covid$hhsize[i])) {
if (covid$hhsize[i] <= 2.35) {
hhsize_c[i] <- "Small"
} else if (covid$hhsize[i] > 2.35 && covid$hhsize[i] < 2.63) {
hhsize_c[i] <- "Mid"
}
else if (covid$hhsize[i] >= 2.63 ) {
hhsize_c[i] <- "Large"
}
}
}
hhsize_c <- as.factor(hhsize_c)
table(hhsize_c)
#Change hhsize into cateogiral and delete original hhsize
covid$hhsize_c <- hhsize_c
covid <- covid[, -7]
#backwards elimination
#install.packages('leaps')
library(leaps)
b <- regsubsets(x = pcases ~ ., data = covid, y = covid$pcases, method= "backward")
summary(b)
#forward model
f <- regsubsets(x = pcases ~ ., data = covid, y = covid$pcases, method= "forward")
summary(f)
covidf <- covid[, c(11,3, 8, 12, 17, 18, 20, 21, 23, 25)] #significant cols
covidb <- covid[, c(11,3, 8, 12,17, 18, 20, 21, 23)] #significant cols
#COVID BACKWARDS MODEL ADJUSTMENTS
########
#logarithmic transformations (covidb)
library(car)
symbox(~pcases, data = covid)
covidb$pcases <- log(covid$pcases)
symbox(~dist, data = covid)
covidb$dist = log(covid$dist)
symbox(~black_pct, data = covid)
covidb$black_pct <- log(covid$black_pct)
symbox(~hisp_pct, data = covid)
covidb$hisp_pct <- log(covid$hisp_pct)
symbox(~wfh_share, data = covid)
covidb$wfh_share <- log(covid$wfh_share)
symbox(~italy_born, data = covid)
covidb$italy_born <- log(covid$italy_born)
symbox(~transit_modeshare, data = covid)
covidb$transit_modeshare <- log(covid$transit_modeshare)
symbox(~age60plus, data = covid)
covid$age60plus <- log(covid$age60plus)
#removing NA vals
#install.packages('IDPmisc')
library(IDPmisc)
covidb <- NaRV.omit(covidb)
#creating backwards-elim mlr model
modelb = lm(formula = pcases ~ temp_mar20+dist+age60plus+black_pct+hisp_pct+wfh_share+italy_born+transit_modeshare+hhsize_c*age60plus, data = covid)
summary(modelb)
library(car)
library(effects)
install.packages("effects")
library(effects)
plot(allEffects(modelb),ask=FALSE)
#Check if there the interaction effect is significant or not
model_interaction <- lm(pcases~hhsize_c+age60plus+hhsize_c*age60plus,data=covid)
plot(allEffects(model_interaction),ask=FALSE)
summary(model_interaction)
#need to remove all non statistically-significant data
modelb = lm(formula = pcases ~ temp_mar20 + dist + black_pct + hisp_pct + transit_modeshare + interaction, data = covidb)
#rewritten modelb w/ all sig data
summary(modelb)
#plot(modelb)
#COVID FORWARD MODEL ADJUSTMENTS
####
#same process for forward process (covidf)
#logarithmic transformations
#symbox(...)
#needed to log following values
covidf$pcases <- log(covid$pcases)
covidf$dist = log(covid$dist)
covidf$age60plus = log(covid$age60plus)
covidf$black_pct <- log(covid$black_pct)
covidf$hisp_pct <- log(covid$hisp_pct)
covidf$wfh_share <- log(covid$wfh_share)
covidf$italy_born <- log(covid$italy_born)
covidf$transit_modeshare <- log(covid$transit_modeshare)
#removing NA vals
#install.packages('IDPmisc')
library(IDPmisc)
covidf <- NaRV.omit(covidf)
#creating forwards-elim mlr model
modelf = lm(formula = pcases ~ ., data = covidf)
summary(modelf)
#need to remove all non-statistically significant data
modelf = lm(formula = pcases ~ temp_mar20 + dist + black_pct + hisp_pct + transit_modeshare + hhsize_c, data = covidf)
#rewritten modelf with all sig data
summary(modelf)
#plot(modelf)
###
covidf$italy_born <- covid$italy_born
covidf$italy_born <- NaRV.omit(covidf$italy_born)
model2 <- lm(formula = pcases ~ temp_mar20 + dist + black_pct + hisp_pct + transit_modeshare + hhsize_c+italy_born, data = covidf)
summary(model2)
View(covidf)
####CORRELATION MATRIX W/ BOTH MODELS
cmat <- cor(cbind(fitted(modelb),fitted(modelf)))
rownames(cmat) <- colnames(cmat) <-c("backward","forward")
cmat
#The correlation between the backwards and forwards model is nearly perfect
#thus either model works equally well in terms of predictive power
#WE WILL NOW DO CROSS-VALIDATION ON OUR BACKWARD MODEL (either model works)
#CROSS VALIDATION STEP:
#CREATING TRAINING SET AND TEST SET
######
# Splitting the dataset into the Training set and Test set
# install.packages('caTools')
library(caTools)
split = sample.split(covidb$pcases, SplitRatio = 0.7)
training_set = subset(covidb, split == TRUE)
test_set = subset(covidb, split == FALSE)
modelb = lm(formula = pcases ~ temp_mar20 + dist + black_pct + hisp_pct + transit_modeshare + interaction, data = training_set)
#revised modelb on just the training_set
y_pred = predict(modelb, newdata = test_set)
cor(y_pred, test_set$pcases)
#Correlation between pcases computed on the base of the training model and the actual pcases in the testing sample is pretty high indicating good prediction power.
#CROSS VALIDATION STEP:
#CREATING TRAINING SET AND TEST SET
######
# Splitting the dataset into the Training set and Test set
# install.packages('caTools')
library(caTools)
split = sample.split(covidf$pcases, SplitRatio = 0.7)
training_set = subset(covidf, split == TRUE)
test_set = subset(covidf, split == FALSE)
modelf = lm(formula = pcases ~ ., data = training_set)
#revised modelb on just the training_set
y_pred = predict(modelf, newdata = test_set)
cor(y_pred, test_set$pcases)
#Correlation between pcases computed on the base of the training model and the actual pcases in the testing sample is pretty high indicating good prediction power.
#final plot
plot(modelf)
summary(modelf)
## LOGISTICS MODEL
summary(covid$pcases)
covid$pcasesCut <- cut(covid$pcases,br=c(0,85.4,12087.9),labels= c(0,1),right=F)
m1<-glm(pcasesCut~dist, family=binomial,data= covid)
summary(m1)
plot(m1)
hist(covid$dist)
dist_log <- log(covid$dist)
hist(dist_log)