我想用下一个数据估计函数的两个断点:
df = data.frame (x = 1:180,
y = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 2, 2, 4, 2, 2, 3, 2, 1, 2,0, 1, 0, 1, 4, 0, 1, 2, 3, 1, 1, 1, 0, 2, 0, 3, 2, 1, 1, 1, 1, 5, 4, 2, 1, 0, 2, 1, 1, 2, 0, 0, 2, 2, 1, 1, 1, 0, 0, 0, 0,
2, 3, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
# plotting y ~ x
plot(df)
我知道该函数具有两个断点,例如:
y = y1 if x < b1;
y = y2 if b1 < x < b2;
y = y3 if b2 < x;
我想找到b1
和b2
以符合以下形式的矩形函数
谁能帮助我或为我指明正确的方向?谢谢!
答案 0 :(得分:3)
1)kmeans 像这样尝试kmeans
:
set.seed(123)
km <- kmeans(df, 3, nstart = 25)
> fitted(km, "classes") # or equivalently km$cluster
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[38] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[112] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[149] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
> unique(fitted(km, "centers")) # or except for order km$centers
x y
3 30.5 0.5166667
1 90.5 0.9000000
2 150.5 0.0000000
> # groups are x = 1-60, 61-120 and 121-180
> simplify2array(tapply(df$x, km$cluster, range))
1 2 3
[1,] 61 121 1
[2,] 120 180 60
plot(df, col = km$cluster)
lines(fitted(km)[, "y"] ~ x, df)
2)蛮力另一种方法是蛮力方法,其中我们计算每个可能的断点对,并选择线性模型中平方和最小的那对。
grid <- subset(expand.grid(b1 = 1:180, b2 = 1:80), b1 < b2)
# the groups are [1, b1], (b1, b2], (b2, Inf)
fit <- function(b1, b2, x, y) {
grp <- factor((x > b1) + (x > b2))
lm(y ~ grp)
}
dv <- function(...) deviance(fit(...))
wx <- which.min(mapply(dv, grid$b1, grid$b2, MoreArgs = df))
grid[wx, ]
## b1 b2
## 14264 44 80
plot(df)
lines(fitted(fit(grid$b1[wx], grid$b2[wx], x, y)) ~ x, df)
答案 1 :(得分:1)
我可以看到y是整数,所以最好用泊松或二项式模型来估计。这是使用R软件包mcp
的解决方案:
# Three intercept segments
model = list(
y ~ 1,
~ 1,
~ 1
)
library(mcp)
fit = mcp(model, df, family = poisson(), par_x = "x", adapt = 2000)
plot(fit)
请注意,mcp
是估计变更点蚂蚁参数估计值周围的不确定性的仅有软件包之一。摘要显示了更改点估计为(cp_1
和cp_2
的位置以及其他参数(对数刻度,因为这是Poisson模型的默认链接函数):
summary(fit)
Family: poisson(link = 'log')
Iterations: 9000 from 3 chains.
Segments:
1: y ~ 1
2: y ~ 1 ~ 1
3: y ~ 1 ~ 1
Population-level parameters:
name mean lower upper Rhat n.eff
cp_1 39.57 37.8 45.00 1 54
cp_2 99.82 99.0 101.21 1 2211
int_1 -4.00 -6.5 -1.88 1 577
int_2 0.32 0.1 0.54 1 6288
int_3 -11.02 -20.9 -3.56 1 2487