-
Notifications
You must be signed in to change notification settings - Fork 0
/
curves.R
168 lines (109 loc) · 4.84 KB
/
curves.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
library(pixiedust)
library(broom)
library(dplyr)
df <- read.csv("./sample_data.csv")
## model linear
lin.lm <- lm(sum ~ days, data = df)
## plot
plot(df$days, df$sum, pch=16, main="linear", ylab = "Sum of revenue", xlab="days", cex.lab=1.5, cex.main=1.5)
abline(lin.lm, col="red", lwd=3)
legend("topleft", bty="n", legend=paste("R2 is", format(summary(lin.lm)$r.squared,digits=4)), cex = 2.0)
## table of statistics
stats = round(glance(lin.lm), 3)
ftable(stats)
## timeseries for plotting
times <- seq(0, length(df$days), 0.1)
## model quadratic
days2 = (df$days)^2
quad.lm <- lm(sum ~ days + days2, data = df)
pred.quad <- predict(quad.lm, list(days=times, days2=times^2))
## plot
plot(df$days, df$sum, pch=16, main="quadratic", ylab = "Sum of revenue", xlab="days", cex.lab=1.5, cex.main=1.5)
lines(times, pred.quad, col = "red", lwd = 3)
legend("topleft", bty="n", legend=paste("R2 is", format(summary(quad.lm)$r.squared,digits=4)), cex = 2.0)
## model cubic
days2 = (df$days)^2
days3 = (df$days)^3
cube.lm <- lm(sum ~ days + days2 + days3, data = df)
pred.cube <- predict(cube.lm, list(days=times, days2=times^2, days3=times^3))
## plot
plot(df$days, df$sum, pch=16, main="cubic", ylab = "Sum of revenue", xlab="days",cex.lab=1.5, cex.main=1.5)
lines(times, pred.cube, col = "red", lwd = 3)
legend("topleft", bty="n", legend=paste("R2 is", format(summary(cube.lm)$r.squared,digits=4)), cex = 2.0)
## model logarithmic
log.lm <- lm(sum ~ log(days), data = df)
pred.log <- predict(log.lm, list(days=times))
## plot
plot(df$days, df$sum, pch=16, main="logarithmic", ylab = "Sum of revenue", xlab="days",cex.lab=1.5, cex.main=1.5)
lines(times, pred.log, col = "red", lwd = 3)
legend("topleft", bty="n", legend=paste("R2 is", format(summary(log.lm)$r.squared,digits=4)), cex = 2.0)
## model (exponential, power)
exp.lm <- lm(log(sum) ~ days, data =df)
pred.exp <- exp(predict(exp.lm, list(days=times)))
## plot
plot(df$days, df$sum, pch=16, main="exponential", ylab = "Sum of revenue", xlab="days",cex.lab=1.5, cex.main=1.5)
lines(times, pred.exp, col="red", lwd=3)
legend("topleft", bty="n", legend=paste("R2 is", format(summary(exp.lm)$r.squared,digits=4)), cex = 2.0)
## model polynomial 2th degree
poly2.lm <- lm(sum ~ poly(days, 2), data =df)
pred.poly2 <- predict(poly2.lm, list(days=times))
## plot
plot(df$days, df$sum, pch=16, main="exponential", ylab = "Sum of revenue", xlab="days",cex.lab=1.5, cex.main=1.5)
lines(times, pred.poly2, col="red", lwd=3)
legend("topleft", bty="n", legend=paste("R2 is", format(summary(poly2.lm)$r.squared,digits=4)), cex = 2.0)
## model polynomial 3th degree
poly3.lm <- lm(sum ~ poly(days, 3), data =df)
pred.poly3 <- predict(poly3.lm, list(days=times))
## plot
plot(df$days, df$sum, pch=16, main="exponential", ylab = "Sum of revenue", xlab="days",cex.lab=1.5, cex.main=1.5)
lines(times, pred.poly3, col="red", lwd=3)
legend("topleft", bty="n", legend=paste("R2 is", format(summary(poly3.lm)$r.squared,digits=4)), cex = 2.0)
## Extracting R squared and coefficients
mod.list = list(lin.lm, quad.lm, cube.lm, log.lm, exp.lm, poly2.lm, poly3.lm)
mod.names = c("linear", "quadratic", "cubic", "logarithmic", "exponential","polynomial2", "polynomial3")
result.list = list()
for (i in seq(mod.list)) {
stats = round(glance(mod.list[[i]]), 3)
coeff = as.data.frame(dust(mod.list[[i]]) %>% sprinkle(round = 3))
R.squared = stats$r.squared
Intercept = coeff[1, 'estimate']
Beta = coeff[2:nrow(coeff), 'estimate']
Predictor = coeff[2:nrow(coeff), 'term']
model.values = cbind(model = mod.names[i], Predictor, Beta, Intercept, R.squared)
result.list[[i]] = model.values
}
## collect all values into table
result.table = do.call(rbind, result.list)
print(result.table)
## Best model
result.df = data.frame(result.table)
index.best = which.max(unique(result.df$R.squared))
best.name = as.character(unique(result.df$model)[index.best])
index.select = which(result.df$model %in% best.name)
best = result.df[index.select, ]
ftable(as.matrix(best))
## Final result at 60 days
Intercept = as.numeric(as.character(unique(best$Intercept)))
Beta = as.numeric(as.character(best$Beta))
Max.Day = 60
if (length(Beta) == 1) {
forecast = Intercept + Beta[1]*Max.Day
}
if (length(Beta) == 2) {
forecast = Intercept + Beta[1]*Max.Day + Beta[2]*Max.Day
}
if (length(Beta) == 3) {
forecast = Intercept + Beta[1]*Max.Day + Beta[2]*Max.Day + Beta[3]*Max.Day
}
print(forecast)
## Polynomial forecast
poly2 = result.df[9:10, ]
Intercept = as.numeric(as.character(unique(poly2$Intercept)))
Beta = as.numeric(as.character(poly2$Beta))
forecast = Intercept + Beta[1]*Max.Day + Beta[2]*Max.Day
print(forecast)
poly3 = result.df[11:13, ]
Intercept = as.numeric(as.character(unique(poly3$Intercept)))
Beta = as.numeric(as.character(poly3$Beta))
forecast = Intercept + Beta[1]*Max.Day + Beta[2]*Max.Day + Beta[3]*Max.Day
print(forecast)