-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbike.R
162 lines (154 loc) · 5.88 KB
/
bike.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
##1
bike = read.csv("http://www.yurulin.com/class/spring2016_datamining/data/bike.csv", header = TRUE, na.strings = c('?'))
##2.a
Rowname = c("temp","atemp","hum","windspeed","cnt")
Columnname =c("min","1st quartile","median","mean","3rd quartile","max","standar devitation")
temp = c(as.vector(summary(bike$temp)), sd(bike$temp))
atemp = c(as.vector(summary(bike$atemp)), sd(bike$atemp))
hum = c(as.vector(summary(bike$hum)), sd(bike$hum))
windspeed = c(as.vector(summary(bike$windspeed)), sd(bike$windspeed))
cnt = c(as.vector(summary(bike$cnt)), sd(bike$cnt))
summary = matrix(c(temp, atemp, hum, windspeed, cnt),nrow = 5, ncol = 7, byrow = TRUE, dimnames = list(Rowname, Columnname))
View(summary)
##2.b
##density distribution
library(ggplot2)
ggplot(bike, aes(x = temp)) + geom_density()
ggplot(bike, aes(x = atemp)) + geom_density()
ggplot(bike, aes(x = hum)) + geom_density()
ggplot(bike, aes(x = windspeed)) + geom_density()
ggplot(bike, aes(x = cnt)) + geom_density()
##2.c
##correlation and scatterplot
cor(bike$temp, bike$cnt)
ggplot(bike, aes(x=temp, y=cnt)) + geom_point(shape=1) +geom_smooth(method=lm, se = FALSE)
cor(bike$atemp, bike$cnt)
ggplot(bike, aes(x=atemp, y=cnt)) + geom_point(shape=1) +geom_smooth(method=lm, se = FALSE)
cor(bike$hum, bike$cnt)
ggplot(bike, aes(x=hum, y=cnt)) + geom_point(shape=1) +geom_smooth(method=lm, se = FALSE)
cor(bike$windspeed, bike$cnt)
ggplot(bike, aes(x=windspeed, y=cnt)) + geom_point(shape=1) +geom_smooth(method=lm, se = FALSE)
##2.d
##conditional density plot
bike$cnt = as.numeric(bike$cnt)
bike$season = as.factor(bike$season)
bike$yr = as.factor(bike$yr)
bike$mnth = as.factor(bike$mnth)
bike$holiday = as.factor(bike$holiday)
bike$weekday = as.factor(bike$weekday)
bike$weathersit = as.factor(bike$weathersit)
ggplot(data=bike, aes(x = cnt, fill = season)) + geom_density(alpha=0.5)
ggplot(data=bike, aes(x = cnt, fill = yr)) + geom_density(alpha=0.5)
ggplot(data=bike, aes(x = cnt, fill = mnth)) + geom_density(alpha=0.5)
ggplot(data=bike, aes(x = cnt, fill = holiday)) + geom_density(alpha=0.5)
ggplot(data=bike, aes(x = cnt, fill = weekday)) + geom_density(alpha=0.5)
ggplot(data=bike, aes(x = cnt, fill = weathersit)) + geom_density(alpha=0.5)
##2.e
library(doBy)
library(survival)
library(splines)
summaryBy(bike$cnt~bike$holiday, data=bike, FUN = c(sum, mean, sd, var, length))
holiday0 = subset(bike, holiday == "0", select = cnt)
holiday1 = subset(bike, holiday == "1", select = cnt)
t.test(holiday0, holiday1, paired = FALSE, var.equal = FALSE, conf.level =0.95)
##3.a
##standard linear regression
##model 1: with all predictors
lm_cnt = lm(cnt ~ ., data = bike)
summary(lm_cnt)
rmse = sqrt(mean(residuals(lm_cnt)^2))
rmse
n = length(bike$cnt)
error = dim(n)
for (k in 1:n) {
train1 = c(1:n)
train = train1[train1 != k]
m1 = lm(cnt ~ ., data = bike[train, ])
pred = predict(m1, newdata = bike[-train, ])
obs = bike$cnt[-train]
error[k] = obs - pred
}
lm1_me = mean(error)
lm1_rmse = sqrt(mean(error^2))
lm1_me
lm1_rmse
##3.b
##optimize linear model and try non-linear model
##model 2: linear
library (MASS)
stepAIC(lm_cnt, direction = "backward")
n = length(bike$cnt)
error = dim(n)
for (k in 1:n) {train1 = c(1:n)
train = train1[train1 != k]
m2 = lm(cnt ~ weekday + holiday + hum + windspeed + mnth + season + temp + weathersit + yr, data = bike[train, ])
pred = predict(m2, newdata = bike[-train, ])
obs = bike$cnt[-train]
error[k] = obs - pred
}
lm2_me = mean(error)
lm2_rmse = sqrt(mean(error^2))
lm2_me
lm2_rmse
##model 3:non-linear
poly1 = lm(cnt ~ weekday + holiday + poly(hum, degree = 2) + poly(windspeed, degree = 2) + mnth + season + poly(temp, degree = 2) + weathersit + yr, data = bike)
n = length(bike$cnt)
error = dim(n)
for (k in 1:n) {
train1 = c(1:n)
train = train1[train1 != k]
m3 = lm(cnt ~ weekday + holiday + poly(hum, degree = 2) + poly(windspeed, degree = 2) + mnth + season + poly(temp, degree = 2) + weathersit + yr, data = bike[train,])
pred = predict(m3, newdat = bike[-train, ])
obs = bike$cnt[-train]
error[k] = obs - pred
}
poly1_me = mean(error)
poly1_rmse = sqrt(mean(error^2))
poly1_rmse
##model 4:non-linear
poly2 = lm(cnt ~ weekday + holiday + poly(hum, degree = 3) + poly(windspeed, degree = 3) + mnth + season + poly(temp, degree = 3) + weathersit + yr, data = bike)
n = length(bike$cnt)
error = dim(n)
for (k in 1:n) {
train1 = c(1:n)
train = train1[train1 != k]
m4 = lm(cnt ~ weekday + holiday + poly(hum, degree = 3) + poly(windspeed, degree = 3) + mnth + season + poly(temp, degree = 3) + weathersit + yr, data = bike[train,])
pred = predict(m4, newdat = bike[-train, ])
obs = bike$cnt[-train]
error[k] = obs - pred
}
poly2_me = mean(error)
poly2_rmse = sqrt(mean(error^2))
poly2_rmse
##model 5:non-linear
poly3 = lm(cnt ~ weekday + holiday + poly(hum, degree = 4) + poly(windspeed, degree = 4) + mnth + season + poly(temp, degree = 4) + weathersit + yr, data = bike)
n = length(bike$cnt)
error = dim(n)
for (k in 1:n) {
train1 = c(1:n)
train = train1[train1 != k]
m5 = lm(cnt ~ weekday + holiday + poly(hum, degree = 4) + poly(windspeed, degree = 4) + mnth + season + poly(temp, degree = 4) + weathersit + yr, data = bike[train,])
pred = predict(m5, newdat = bike[-train, ])
obs = bike$cnt[-train]
error[k] = obs - pred
}
poly3_me = mean(error)
poly3_rmse = sqrt(mean(error^2))
poly3_rmse
##model 6:non-linear
poly4 = lm(cnt ~ weekday + holiday + poly(hum, degree = 5) + poly(windspeed, degree = 5) + mnth + season + poly(temp, degree = 5) + weathersit + yr, data = bike)
n = length(bike$cnt)
error = dim(n)
for (k in 1:n) {
train1 = c(1:n)
train = train1[train1 != k]
m6 = lm(cnt ~ weekday + holiday + poly(hum, degree = 5) + poly(windspeed, degree = 5) + mnth + season + poly(temp, degree = 5) + weathersit + yr, data = bike[train,])
pred = predict(m6, newdat = bike[-train, ])
obs = bike$cnt[-train]
error[k] = obs - pred
}
poly4_me = mean(error)
poly4_rmse = sqrt(mean(error^2))
poly4_rmse
##3.c
summary(poly3)