-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6-Others.R
209 lines (193 loc) · 5.35 KB
/
6-Others.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
library(car)
library(mgcv)
library(psych)
library(dplyr)
library(mgcViz)
library(spdep)
library(sf)
library(tmap)
library(leaps)
library(MASS)
library(glmnet)
library(mdatools)
# Read data
dat <- read.csv('D:/Transit/All_final_Transit_R2.csv')
dat$rides <- round(dat$rides)
# Stepwise
tmp_Impact <- step(lm(
Relative_Impact ~ COMMERCIAL + # rides
INDUSTRIAL +
INSTITUTIONAL +
OPENSPACE +
RESIDENTIAL +
Cumu_Cases +
Pct.Male +
Pct.Age_0_24 +
Pct.Age_25_40 +
Pct.Age_40_65 +
Pct.White +
Pct.Black +
Income +
PopDensity +
Freq +
Num_trips +
Pct.WJob_Utilities +
Pct.WJob_Goods_Product +
WTotal_Job_Density,
data = dat), direction = "both")
summary(tmp_Impact)
tmp_Rides <- step(glm.nb(
rides ~ COMMERCIAL + # rides
INDUSTRIAL +
INSTITUTIONAL +
OPENSPACE +
RESIDENTIAL +
Cumu_Cases +
Pct.Male +
Pct.Age_0_24 +
Pct.Age_25_40 +
Pct.Age_40_65 +
Pct.White +
Pct.Black +
Income +
PopDensity +
Freq +
Num_trips +
Pct.WJob_Utilities +
Pct.WJob_Goods_Product +
WTotal_Job_Density, data = dat), direction = "both")
summary(tmp_Rides)
# Use the selection to build gam
dat$station_id <- as.factor(dat$station_id)
colnames(dat)
GAM_RES1 <-
mgcv::gam(Relative_Impact ~
COMMERCIAL + # rides
INDUSTRIAL +
INSTITUTIONAL +
OPENSPACE +
RESIDENTIAL +
Cumu_Cases +
Pct.Male +
Pct.Age_0_24 +
Pct.Age_25_40 +
Pct.Age_40_65 +
Pct.White +
Pct.Black +
Income +
PopDensity +
Freq +
Num_trips +
Pct.WJob_Utilities +
Pct.WJob_Goods_Product +
WTotal_Job_Density +
ti(LAT, LNG),
data = dat, family = c("gaussian"), method = "REML")
summary(GAM_RES1)
concurvity(GAM_RES1, full = FALSE)
GAM_RES2 <-
mgcv::gam(rides ~
COMMERCIAL + # rides
INDUSTRIAL +
INSTITUTIONAL +
OPENSPACE +
RESIDENTIAL +
Cumu_Cases +
Pct.Male +
Pct.Age_0_24 +
Pct.Age_25_40 +
Pct.Age_40_65 +
Pct.White +
Pct.Black +
Income +
PopDensity +
Freq +
Num_trips +
Pct.WJob_Utilities +
Pct.WJob_Goods_Product +
WTotal_Job_Density +
ti(LAT, LNG),
data = dat, family = 'nb', method = "REML")
summary(GAM_RES2)
# LASSO/RIDGE
lambdas <- 10^seq(5, -5, by = -.1)
x <- dat %>%
dplyr::select(COMMERCIAL, INDUSTRIAL, INSTITUTIONAL, OPENSPACE, RESIDENTIAL,
Cumu_Cases, Pct.Male, Pct.Age_0_24, Pct.Age_25_40, Pct.Age_40_65,
Pct.White, Pct.Black, Income, PopDensity, Freq, Num_trips, Pct.WJob_Utilities, Pct.WJob_Goods_Product,
WTotal_Job_Density) %>%
data.matrix()
## For impact
y <- dat$Relative_Impact
# RIDGE
cv_fit <- cv.glmnet(x, y, alpha = 0, lambda = lambdas)
plot(cv_fit)
opt_lambda <- cv_fit$lambda.min
# Rebuilding the model with optimal lambda value
best_ridge <- glmnet(x, y, alpha = 0, lambda = cv_fit$lambda.min)
coef(best_ridge)
# LASSO
cv_fit <- cv.glmnet(x, y, alpha = 1, lambda = lambdas)
plot(cv_fit)
opt_lambda <- cv_fit$lambda.min
# Rebuilding the model with optimal lambda value
best_lasso <- glmnet(x, y, alpha = 1, lambda = cv_fit$lambda.min)
coef(best_lasso)
## For ride
y <- dat$rides
# RIDGE
cv_fit <- cv.glmnet(x, y, alpha = 0, lambda = lambdas, family = "poisson")
plot(cv_fit)
opt_lambda <- cv_fit$lambda.min
# Rebuilding the model with optimal lambda value
best_ridge <- glmnet(x, y, alpha = 0, lambda = cv_fit$lambda.min, family = "poisson")
coef(best_ridge)
# LASSO
cv_fit <- cv.glmnet(x, y, alpha = 1, lambda = lambdas, family = "poisson")
plot(cv_fit)
opt_lambda <- cv_fit$lambda.min
# Rebuilding the model with optimal lambda value
best_lasso <- glmnet(x, y, alpha = 1, lambda = cv_fit$lambda.min)
coef(best_lasso)
# Use the selection to build gam
dat$station_id <- as.factor(dat$station_id)
colnames(dat)
GAM_RES1 <-
mgcv::gam(Relative_Impact ~
COMMERCIAL +
INDUSTRIAL +
Pct.Age_25_40 +
Pct.Age_40_65 +
Pct.White +
Pct.Black +
Income +
PopDensity +
Pct.WJob_Utilities +
WTotal_Job_Density +
ti(LAT, LNG),
data = dat, family = c("gaussian"), method = "REML")
summary(GAM_RES1)
GAM_RES2 <-
mgcv::gam(rides ~
COMMERCIAL +
INDUSTRIAL +
Pct.Age_25_40 +
Pct.Age_40_65 +
PopDensity +
Freq +
Num_trips +
Pct.White +
Pct.Black +
Income +
WTotal_Job_Density +
Pct.WJob_Utilities +
ti(LAT, LNG),
data = dat, family = c("nb"), method = "REML")
summary(GAM_RES2)
# PCA/PLS
m <- pls(x, y, 7, cv = 4, scale = TRUE, info = "Shoesize prediction model")
summary(m)
plotRegcoeffs(m)
show(m$coeffs$values[, 3, 1])
summary(m$coeffs)
m <- selectCompNum(m, 3)