forked from WinVector/RcppDynProg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentationL.Rmd
More file actions
188 lines (158 loc) · 6.45 KB
/
SegmentationL.Rmd
File metadata and controls
188 lines (158 loc) · 6.45 KB
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
---
title: "Linear Segmentation"
author: "John Mount"
date: "`r Sys.Date()`"
output: github_document
---
In this example we fit a piecewise linear function to example data.
Please see [here](https://github.com/WinVector/RcppDynProg) for a discussion of the methodology.
```{r r1, fig.height = 6, fig.width = 8, fig.align = "center"}
library("RcppDynProg")
library("ggplot2")
set.seed(2018)
g <- 100
d <- data.frame(
x = 0.05*(1:(3*g))) # ordered in x
d$y_ideal <- sin((0.3*d$x)^2)
d$y_observed <- d$y_ideal + 0.25*rnorm(length(d$y_ideal))
plt1 <- ggplot(data= d, aes(x = x)) +
geom_line(aes(y = y_ideal), linetype=2) +
geom_point(aes(y = y_observed)) +
ylab("y") +
ggtitle("raw data",
subtitle = "dots: observed values, dashed line: unobserved true values")
print(plt1)
x_cuts <- solve_for_partition(d$x, d$y_observed, penalty = 1)
print(x_cuts)
d$estimate <- approx(x_cuts$x, x_cuts$pred, xout = d$x, method = "linear", rule = 2)$y
d$group <- as.character(findInterval(d$x, x_cuts[x_cuts$what=="left", "x"]))
```
```{r r2, fig.height = 6, fig.width = 8, fig.align = "center"}
print(sum((d$y_observed - d$y_ideal)^2))
```
```{r r3, fig.height = 6, fig.width = 8, fig.align = "center"}
print(sum((d$estimate - d$y_ideal)^2))
```
```{r r4, fig.height = 6, fig.width = 8, fig.align = "center"}
print(sum((d$estimate - d$y_observed)^2))
```
```{r r5, fig.height = 6, fig.width = 8, fig.align = "center"}
plt2 <- ggplot(data= d, aes(x = x)) +
geom_line(aes(y = y_ideal), linetype=2) +
geom_point(aes(y = y_observed, color = group)) +
geom_line(aes(y = estimate, color = group)) +
ylab("y") +
ggtitle("RcppDynProg piecewise linear estimate",
subtitle = "dots: observed values, segments: observed group means, dashed line: unobserved true values") +
theme(legend.position = "none")
print(plt2)
```
```{r r6, fig.height = 6, fig.width = 8, fig.align = "center"}
use_vtreat <- requireNamespace("vtreat", quietly = TRUE)
```
Here we show many of the [`vtreat`](https://github.com/WinVector/vtreat) [custom variable coders](http://www.win-vector.com/blog/2017/09/custom-level-coding-in-vtreat/).
An application of these coders can be found [here](https://github.com/WinVector/zmPDSwR/blob/master/KDD2009/KDD2009vtreat.md).
```{r r7, fig.height = 6, fig.width = 8, fig.align = "center", eval=use_vtreat}
spline_variable <- function(varName, x, y, w = NULL) {
if(is.null(w)) {
w <- numeric(n) + 1
}
nknots <- 100
spline <- stats::smooth.spline(x, y,
w = w,
nknots = nknots,
keep.data = FALSE,
keep.stuff = FALSE,
cv = TRUE)$fit
estimate <- stats::predict(spline, x)$y
return(estimate)
}
customCoders = list('c.PiecewiseV.num' = vtreat::solve_piecewise,
'n.PiecewiseV.num' = vtreat::solve_piecewise,
'c.knearest.num' = vtreat::square_window,
'n.knearest.num' = vtreat::square_window,
'c.spline.num' = spline_variable,
'n.spline.num' = spline_variable,
'c.PiecewiseLin.num' = RcppDynProg::piecewise_linear,
'n.PiecewiseLin.num' = RcppDynProg::piecewise_linear,
'c.PiecewiseC.num' = RcppDynProg::piecewise_constant,
'n.PiecewiseC.num' = RcppDynProg::piecewise_constant)
codeRestriction = c("PiecewiseV",
"knearest",
"spline",
"PiecewiseLin",
"PiecewiseC")
trt <- vtreat::designTreatmentsN(
d,
'x', 'y_observed',
customCoders = customCoders,
codeRestriction = codeRestriction,
verbose = FALSE)
knitr::kable(trt$scoreFrame[, c("varName", "rsq", "sig")])
dt <- vtreat::prepare(trt, d)
dt$y_observed <- NULL
d <- cbind(d, dt)
```
```{r r8, fig.height = 6, fig.width = 8, fig.align = "center", eval=use_vtreat}
print(sum((d$x_PiecewiseV - d$y_ideal)^2))
print(sum((d$x_PiecewiseV - d$y_observed)^2))
```
```{r r9, fig.height = 6, fig.width = 8, fig.align = "center", eval=use_vtreat}
print(sum((d$x_knearest - d$y_ideal)^2))
print(sum((d$x_knearest - d$y_observed)^2))
```
```{r r10, fig.height = 6, fig.width = 8, fig.align = "center", eval=use_vtreat}
print(sum((d$x_PiecewiseLin - d$y_ideal)^2))
print(sum((d$x_PiecewiseLin - d$y_observed)^2))
```
```{r r11, fig.height = 6, fig.width = 8, fig.align = "center", eval=use_vtreat}
print(sum((d$x_spline - d$y_ideal)^2))
print(sum((d$x_spline - d$y_observed)^2))
```
```{r r12, fig.height = 6, fig.width = 8, fig.align = "center", eval=use_vtreat}
plt3 <- ggplot(data= d, aes(x = x)) +
geom_line(aes(y = y_ideal), linetype=2) +
geom_point(aes(y = y_observed)) +
geom_line(aes(y = x_PiecewiseV)) +
ylab("y") +
ggtitle("vtreat PiecewiseV estimate",
subtitle = "dots: observed values, segments: observed group means, dashed line: unobserved true values") +
theme(legend.position = "none")
print(plt3)
plt4 <- ggplot(data= d, aes(x = x)) +
geom_line(aes(y = y_ideal), linetype=2) +
geom_point(aes(y = y_observed)) +
geom_line(aes(y = x_knearest)) +
ylab("y") +
ggtitle("vtreat k-nearest estimate",
subtitle = "dots: observed values, segments: observed group means, dashed line: unobserved true values") +
theme(legend.position = "none")
print(plt4)
plt5 <- ggplot(data= d, aes(x = x)) +
geom_line(aes(y = y_ideal), linetype=2) +
geom_point(aes(y = y_observed)) +
geom_line(aes(y = x_PiecewiseLin)) +
ylab("y") +
ggtitle("vtreat RcppDynProg piecewise linear estimate",
subtitle = "dots: observed values, segments: observed group means, dashed line: unobserved true values") +
theme(legend.position = "none")
print(plt5)
plt6 <- ggplot(data= d, aes(x = x)) +
geom_line(aes(y = y_ideal), linetype=2) +
geom_point(aes(y = y_observed)) +
geom_line(aes(y = x_PiecewiseC)) +
ylab("y") +
ggtitle("vtreat RcppDynProg piecewise constant estimate",
subtitle = "dots: observed values, segments: observed group means, dashed line: unobserved true values") +
theme(legend.position = "none")
print(plt6)
plt7 <- ggplot(data= d, aes(x = x)) +
geom_line(aes(y = y_ideal), linetype=2) +
geom_point(aes(y = y_observed)) +
geom_line(aes(y = x_spline)) +
ylab("y") +
ggtitle("spline estimate",
subtitle = "dots: observed values, segments: observed group means, dashed line: unobserved true values") +
theme(legend.position = "none")
print(plt7)
```