åºåç·å½¢å帰ã¢ãã«ã¯ããã¼ã¿ã»ãããè¤æ°ã®åºéã«åå²ããããããã®åºéã§ç·å½¢å帰ã¢ãã«ãé©ç¨ããææ³ã§ãããããã«ããããã¼ã¿ã®å±æçãªå¾åãéç·å½¢ã®ãã¿ã¼ã³ãæãããã¨ãã§ãããããã§ã¯segmentedããã±ã¼ã¸ã使ç¨ããã
# å¿ è¦ãªããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¦ãã¼ã install.packages("segmented") library(segmented)
# ä»®æ³ãã¼ã¿ã®çæ set.seed(123) # çµæã®åç¾æ§ã®ãã x <- 1:100 y <- ifelse(x < 50, 2 * x + rnorm(100, sd = 5), -2 * x + rnorm(100, sd = 5)) plot(x,y)
ã¢ãã«ã®ãã£ãã
# åºæ¬ã®ç·å½¢ã¢ãã«ã®ãã£ãã lm.model <- lm(y ~ x) # Piecewise linear regressionã¢ãã«ã®ãã£ãã pw.model <- segmented(lm.model, seg.Z = ~ x, psi = list(x = 50), control = seg.control(display = FALSE))
çµæã®è¡¨ç¤º
summary(pw.model) plot(x, y) lines(x, predict(pw.model), col = "red")
Estimated Break-Point(s): Est. St.Err psi1.x 33 2.64 Meaningful coefficients of the linear terms: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.0179 14.7908 0.069 0.9453 x 1.9362 0.7591 2.551 0.0123 * U1.x -6.6143 0.8031 -8.236 NA --- Signif. codes: 0 â***â 0.001 â**â 0.01 â*â 0.05 â.â 0.1 â â 1 Residual standard error: 41.52 on 96 degrees of freedom Multiple R-Squared: 0.8478, Adjusted R-squared: 0.8431 Boot restarting based on 6 samples. Last fit: Convergence attained in 2 iterations (rel. change 3.422e-11)
2ã¤ã®å帰å¼ã®ãã¬ã¤ã¯ãã¤ã³ã33ã§ãã£ãã
ãã£ãããããã¢ãã«ã®åãåºåããããé¢ä¿ã®å¾ããè¨ç®ããã
slope(pw.model)
$x Est. St.Err. t value CI(95%).l CI(95%).u slope1 1.9362 0.75909 2.5507 0.42942 3.4430 slope2 -4.6781 0.26230 -17.8350 -5.19870 -4.1574