segmented
包提供了一个函数segmented
,可以适合分段线性回归。以下是语法:
segmented(obj, seg.Z, psi, control = seg.control(),
model = TRUE, ...)
主要是lm
需要obj
,seg.Z
包含自变量,psi
包含断点列表。
我有多个车辆的数据框,包含速度(svel)和Time。每辆车都有一个唯一的ID,称为Vehicle.ID2
。以下是在segmented
(名为Vehicle.ID2=="430-419"
)上使用veh
的数据和代码:
> dput(veh %>% select(Tim, svel))
structure(list(Tim = c(169.7, 169.8, 169.9, 170, 170.1, 170.2,
170.3, 170.4, 170.5, 170.6, 170.7, 170.8, 170.9, 171, 171.1,
171.2, 171.3, 171.4, 171.5, 171.6, 171.7, 171.8, 171.9, 172,
172.1, 172.2, 172.3, 172.4, 172.5, 172.6, 172.7, 172.8, 172.9,
173, 173.1, 173.2, 173.3, 173.4, 173.5, 173.6, 173.7, 173.8,
173.9, 174, 174.1, 174.2, 174.3, 174.4, 174.5, 174.6, 174.7,
174.8, 174.9, 175, 175.1, 175.2, 175.3, 175.4, 175.5, 175.6,
175.7, 175.8, 175.9, 176, 176.1, 176.2, 176.3, 176.4, 176.5,
176.6, 176.7, 176.8, 176.9, 177, 177.1, 177.2, 177.3, 177.4,
177.5, 177.6, 177.7, 177.8, 177.9, 178, 178.1, 178.2, 178.3,
178.4, 178.5, 178.6, 178.7, 178.8, 178.9, 179, 179.1, 179.2,
179.3, 179.4, 179.5, 179.6, 179.7, 179.8, 179.9, 180, 180.1,
180.2, 180.3, 180.4, 180.5, 180.6, 180.7, 180.8, 180.9, 181,
181.1, 181.2, 181.3, 181.4, 181.5, 181.6, 181.7, 181.8, 181.9,
182, 182.1, 182.2, 182.3, 182.4, 182.5, 182.6, 182.7, 182.8,
182.9, 183, 183.1, 183.2, 183.3, 183.4, 183.5, 183.6, 183.7,
183.8, 183.9, 184, 184.1, 184.2, 184.3, 184.4, 184.5, 184.6,
184.7, 184.8, 184.9, 185, 185.1, 185.2), svel = c(21.88, 22.11,
22.18, 22.19, 22.12, 21.94, 21.67, 21.32, 20.94, 20.55, 20.21,
19.93, 19.72, 19.55, 19.4, 19.28, 19.19, 19.13, 19.1, 19.08,
19.1, 19.17, 19.27, 19.4, 19.52, 19.63, 19.72, 19.79, 19.85,
19.92, 19.98, 20.04, 20.08, 20.1, 20.1, 20.09, 20.09, 20.08,
20.06, 20.05, 20.05, 20.09, 20.17, 20.31, 20.49, 20.69, 20.88,
21.04, 21.15, 21.23, 21.27, 21.29, 21.29, 21.28, 21.25, 21.2,
21.12, 20.99, 20.83, 20.68, 20.6, 20.59, 20.62, 20.69, 20.79,
20.91, 21.05, 21.21, 21.38, 21.58, 21.79, 22.01, 22.25, 22.5,
22.75, 23.01, 23.26, 23.52, 23.75, 23.96, 24.13, 24.23, 24.29,
24.32, 24.34, 24.35, 24.36, 24.35, 24.33, 24.29, 24.24, 24.18,
24.11, 24.04, 23.98, 23.93, 23.86, 23.78, 23.66, 23.53, 23.38,
23.24, 23.12, 23.04, 23.01, 23.02, 23.08, 23.17, 23.29, 23.42,
23.56, 23.68, 23.8, 23.92, 24.05, 24.17, 24.28, 24.38, 24.49,
24.62, 24.79, 25, 25.23, 25.45, 25.65, 25.82, 25.96, 26.05, 26.1,
26.11, 26.06, 25.95, 25.8, 25.6, 25.37, 25.15, 24.99, 24.85,
24.74, 24.66, 24.62, 24.64, 24.72, 24.87, 25.08, 25.33, 25.6,
25.86, 26.11, 26.34, 26.55, 26.73, 26.86, 26.96, 27, 26.98)), row.names = c(NA,
-156L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Tim",
"svel"))
my.lm <- lm(svel ~ Tim, data = veh)
library(segmented)
my.seg <- segmented(my.lm,
seg.Z = ~ Tim,
psi = list(Tim = c(171.5, 172.8,173.8,174.6,175.3,175.8,177.7,178.6,180.1,182.6,183.9,185.1)))
您可以在上面的代码中看到速度(svel),时间(Time,而不是完整数据框中的Tim)和psi是必需的。我使用dplyr
来估算数据框lm
中每辆车的df1015
:
> dput(df10152)
structure(list(Vehicle.ID2 = c("6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4",
"6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "6-4", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5", "8-5",
"8-5", "8-5", "8-5", "8-5", "8-5", "8-5"), svel = c(45.16, 45.15,
45.15, 45.16, 45.17, 45.02, 44.78, 44.6, 44.4, 44.15, 43.82,
43.51, 43.24, 42.99, 40.87, 40.87, 40.87, 40.91, 40.96, 41.03,
41.12, 41.22, 41.32, 41.43, 41.52, 41.61, 41.69, 41.76, 41.83,
41.65, 41.58, 41.53, 41.43, 41.36, 41.27, 41.17, 41.07, 40.98,
40.9, 40.86, 40.85, 40.88, 40.91, 40.96, 41, 41.05, 41.11, 41.17,
41.24, 41.32, 41.41, 41.49, 41.57, 41.63, 41.69, 41.73, 41.77,
41.81, 41.84, 41.86, 41.88, 41.89, 41.9, 41.9, 41.89, 41.89,
41.88, 41.87, 41.86, 41.84, 41.83, 41.81, 41.8, 41.78, 41.76,
41.74, 41.73, 41.72, 41.71, 41.7, 41.7, 41.7, 41.7, 41.72, 41.75,
41.8, 41.85, 41.92, 42, 42.09, 42.19, 42.3, 42.41, 42.52, 42.63,
42.73, 42.82, 42.9, 42.96, 43.01, 43.06, 43.09, 43.1, 43.11,
43.12, 43.15, 43.19, 43.25, 43.32, 43.4, 43.5, 43.61, 43.75,
43.9, 44.05, 44.21, 44.34, 44.47, 44.58, 44.67, 44.76, 44.83,
44.9, 44.95, 44.98, 45, 45, 45, 45, 45.01, 45.01, 45.02, 45.03,
45.05, 45.09, 45.13, 45.17, 45.22, 45.27, 45.32, 45.37, 45.43,
45.49, 45.55, 45.61, 45.68, 45.75, 45.83, 45.92, 46.02, 46.13,
46.25, 46.39, 46.54, 46.69, 46.86, 47.03, 47.22, 47.41, 47.59,
47.76, 47.9, 48.03, 48.13, 48.22, 48.28, 48.32, 48.35, 48.36,
48.34, 48.31, 48.27, 48.21, 48.17, 48.14, 48.12, 48.11, 48.1,
48.09, 48.08, 48.09, 48.1, 48.14, 48.19, 48.27, 48.35, 48.45,
48.54, 48.62, 48.68, 48.73, 48.79, 48.84, 48.89, 48.94, 48.99,
49.04, 49.09, 49.15, 49.2, 49.26, 49.32, 49.39, 49.47, 49.56,
49.62, 49.65, 49.66, 49.67, 49.71, 49.76, 49.84, 49.92, 49.99,
50.06, 50.11, 50.15, 50.18, 50.19, 50.22, 50.26, 50.34, 50.45,
50.57, 50.7, 50.83, 50.96, 51.1, 51.24, 51.4, 51.56, 51.73, 51.92,
52.11, 52.3, 52.48, 52.65, 52.85, 53.08, 53.31, 53.53, 53.74,
53.93, 54.1, 54.27, 54.42, 54.58, 54.72, 54.86, 54.99, 55.09,
55.2, 55.3, 55.41, 55.49, 55.55, 55.59, 55.61, 55.61, 55.59,
55.58, 55.57, 55.57, 55.57, 55.58, 55.58, 55.61, 55.68, 55.76,
55.88, 56, 56.1, 56.18, 56.24, 56.28, 56.3, 56.3, 56.26, 56.2,
56.13, 56.08, 56.06, 45, 45, 45, 45, 45.01, 44.96, 44.69, 44.61,
44.62, 44.6, 44.59, 44.54, 44.43, 44.34, 44.21, 44, 43.74, 43.47,
43.21, 42.92, 42.61, 42.27, 41.93, 41.56, 41.17, 40.8, 40.46,
40.11, 39.76, 39.43, 39.11, 38.8, 38.52, 38.27, 38.06, 37.87,
37.69, 37.52, 37.38, 37.27, 37.19, 37.15, 37.13, 37.14, 37.17,
37.22, 37.27, 37.36, 37.51, 37.72, 37.96, 38.22, 38.48, 38.73,
38.98, 39.21, 39.44, 39.66, 39.87, 40.06, 40.23, 40.37, 40.5,
40.6, 40.67, 40.73, 40.77, 40.8, 40.84, 40.88, 40.91, 40.91,
40.87, 40.78, 40.65, 40.5, 40.36, 40.25, 40.18, 40.12, 40.05,
39.98, 39.9, 39.82, 39.72, 39.63, 39.53, 39.42, 39.3, 39.17,
39.01, 38.84, 38.66, 38.47, 38.29, 38.12, 37.97, 37.84, 37.71,
37.6, 37.51, 37.45, 37.41, 37.4, 37.42, 37.47, 37.55, 37.68,
37.82, 37.97, 38.12, 38.25, 38.37, 38.46, 38.57, 38.68, 38.8,
38.91, 39.01, 39.09, 39.16, 39.23, 39.3, 39.36, 39.41, 39.47,
39.51, 39.55, 39.59, 39.62, 39.65, 39.68, 39.7, 39.72, 39.75,
39.77, 39.8, 39.82, 39.83, 39.84, 39.85, 39.85, 39.84, 39.84,
39.83, 39.83, 39.82, 39.81, 39.79, 39.79, 39.79, 39.81, 39.83,
39.85, 39.86, 39.88, 39.89, 39.9, 39.91, 39.93, 39.94, 39.96,
39.97, 39.99, 40.01, 40.02, 40.03, 40.04, 40.05, 40.06, 40.08,
40.09, 40.1, 40.12, 40.14, 40.15, 40.17, 40.19, 40.22, 40.25,
40.28, 40.31, 40.34, 40.38, 40.42, 40.46, 40.51, 40.56, 40.61,
40.66, 40.7, 40.72, 40.72, 40.71, 40.68, 40.63, 40.57, 40.52,
40.48, 40.46, 40.45, 40.44, 40.43, 40.43, 40.44, 40.45, 40.46,
40.48, 40.51, 40.55, 40.59, 40.64, 40.7, 40.77, 40.84, 40.92,
41.02, 41.13, 41.27, 41.43, 41.6, 41.79, 41.99, 42.2, 42.4, 42.59,
42.78, 42.96, 43.12, 43.27, 43.39, 43.5, 43.59, 43.68, 43.77,
43.86, 43.96, 44.06, 44.16, 44.25, 44.34, 44.41, 44.48, 44.54,
44.6, 44.65, 44.69, 44.74, 44.78, 44.81, 44.84, 44.87, 44.9,
44.92, 44.93, 44.94, 44.95, 44.96, 44.98, 44.99, 45.02, 45.05,
45.08, 45.13, 45.17, 45.22, 45.27, 45.32, 45.36, 45.42, 45.47,
45.54, 45.61, 45.69, 45.78, 45.88, 45.98, 46.1, 46.23, 46.37,
46.53, 46.71, 46.91, 47.13, 47.38, 47.65, 47.95, 48.29, 48.65,
49.03, 49.4, 49.76, 50.08, 50.36, 50.59, 50.77, 50.92, 51.06,
51.2, 51.35, 51.52, 51.7, 51.88, 52.08, 52.27, 52.47, 52.68,
52.88, 53.07, 53.23, 53.37, 53.49, 53.59, 53.67, 53.74, 53.8,
53.84, 53.87, 53.89, 53.89, 53.87, 53.84, 53.79, 53.73, 53.66,
53.57, 53.46, 53.33, 53.19, 53.05, 52.92, 52.8, 52.68, 52.58,
52.5, 52.45, 52.44, 52.45, 52.5, 52.56, 52.66, 52.78, 52.94,
53.13, 53.35, 53.58, 53.83, 54.07, 54.3, 54.51, 54.73, 54.95,
55.17, 55.38, 55.57, 55.74, 55.89, 56.04, 56.2, 56.37, 56.55,
56.76, 56.99, 57.24, 57.5, 57.78, 58.04, 58.29, 58.49, 58.65,
58.78, 58.89, 58.99, 59.08, 59.15, 59.21, 59.26, 59.3, 59.31,
59.33, 59.37, 59.44, 59.52, 59.61, 59.68, 59.75, 59.81, 59.88,
59.94, 60.01, 60.08, 60.14, 60.19, 60.23, 60.26, 60.3, 60.33,
60.37, 60.41, 60.46, 60.53, 60.61, 60.69, 60.75, 60.8, 60.84,
60.86, 60.89, 60.92, 60.98, 61.04, 61.1, 61.15, 61.18, 61.21,
61.23, 61.24, 61.25, 61.25, 61.24, 61.21, 61.19, 61.19, 61.22,
61.25, 61.27, 61.27, 61.28, 61.3, 61.27, 61.24), Frame.ID = c(141L,
142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L,
153L, 154L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L,
177L, 178L, 179L, 180L, 181L, 182L, 225L, 226L, 227L, 229L, 230L,
231L, 232L, 233L, 234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L,
242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L,
253L, 254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L,
264L, 265L, 266L, 267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L,
275L, 276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L,
286L, 287L, 288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L,
297L, 298L, 299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L,
308L, 309L, 310L, 311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L,
319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L,
330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L,
341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L,
352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L,
363L, 364L, 365L, 366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L,
374L, 375L, 376L, 377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L,
385L, 386L, 387L, 388L, 389L, 390L, 391L, 392L, 393L, 394L, 395L,
396L, 397L, 398L, 399L, 400L, 401L, 402L, 403L, 404L, 405L, 406L,
407L, 408L, 409L, 410L, 411L, 412L, 413L, 414L, 415L, 416L, 417L,
418L, 419L, 420L, 421L, 422L, 423L, 424L, 425L, 426L, 427L, 428L,
429L, 430L, 431L, 432L, 433L, 434L, 435L, 436L, 437L, 438L, 439L,
440L, 441L, 442L, 443L, 444L, 445L, 446L, 447L, 448L, 449L, 450L,
451L, 452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L, 460L, 461L,
462L, 463L, 464L, 465L, 466L, 467L, 468L, 469L, 470L, 471L, 472L,
473L, 474L, 475L, 476L, 477L, 478L, 29L, 30L, 31L, 32L, 33L,
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L,
86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L,
99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L,
110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L,
121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L,
132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L,
143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L,
154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L,
165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L,
176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L,
187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L,
198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L,
209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L,
220L, 221L, 222L, 223L, 224L, 225L, 226L, 227L, 228L, 229L, 230L,
231L, 232L, 233L, 234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L,
242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L,
253L, 254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L,
264L, 265L, 266L, 267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L,
275L, 276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L,
286L, 287L, 288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L,
297L, 298L, 299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L,
308L, 309L, 310L, 311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L,
319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L,
330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L,
341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L,
352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L,
363L, 364L, 365L, 366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L,
374L, 375L, 376L, 377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L,
385L, 386L, 387L, 388L, 389L, 390L, 391L, 392L, 393L, 394L, 395L,
396L, 397L, 398L, 399L, 400L, 401L, 402L, 403L, 404L, 405L, 406L,
407L, 408L, 409L, 410L, 411L, 412L, 413L, 414L, 415L, 416L, 417L,
418L, 419L, 420L, 421L, 422L, 423L, 424L, 425L, 426L, 427L, 428L,
429L, 430L, 431L, 432L, 433L, 434L, 435L, 436L, 437L, 438L, 439L,
440L, 441L, 442L, 443L, 444L, 445L, 446L, 447L, 448L, 449L, 450L,
451L, 452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L), dssvel = c(NA,
NA, 0, 2, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, -2, 0,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Vehicle.ID2) %>%
mutate(Time = Frame.ID/10) %>%
do(my.lm = lm(svel~Time, data=.)) %>%
ungroup()
由于有数百辆车辆,因此无法在psi
中为每个车辆数据提供segmented
参数。所以,我估算了每辆车的psi
:
psi10152 <- df10152 %>%
mutate(Time = Frame.ID/10) %>%
group_by(Vehicle.ID2) %>%
do(data.frame(psi = .$Time[which(.$dssvel!=0)])) %>%
ungroup()
lm10152 <- left_join(x = psi10152, y = lm10152)
为了将segmented
功能应用于我尝试使用dplyr
的所有车辆:
> df10152 %>%
+ mutate(Time = Frame.ID/10) %>%
+ left_join(x = lm10152, y = .) %>%
+ group_by(Vehicle.ID2) %>%
+ do(my.seg = segmented(my.lm, seg.Z = ~Time, psi=list(Time=.$psi)))
Joining by: "Vehicle.ID2"
Error in segmented(my.lm, seg.Z = ~Time, psi = list(Time = .$psi)) :
object 'my.lm' not found
df10152 %>%
mutate(Time = Frame.ID/10) %>%
left_join(x = lm10152, y = .) %>%
group_by(Vehicle.ID2) %>%
do(my.seg = segmented(lm(svel~Time, data=.), seg.Z = ~Time, psi=list(Time=.$psi)))
这个运行时间太长但没有产生任何结果。我无法弄清楚如何在完整的数据框中使用segmented
函数用于多个车辆。请帮忙。
答案 0 :(得分:1)
我认为您的代码可以大大简化如下:
library(data.table)
dt = as.data.table(yourdf) # or setDT to convert in place
dt[, Time := Frame.ID/10]
dt[, .(segs = list(segmented(lm(svel ~ Time), seg.Z = ~Time,
psi = list(Time = Time[which(dssvel != 0)]))))
, by = Vehicle.ID2]
# Vehicle.ID2 segs
#1: 6-4 <segmented>
#2: 8-5 <segmented>
我将上述内容存储在列表中,因为它是一种复杂的数据类型,data.frame
和data.table
都不会按原样使用。
另请注意,segmented
算法似乎具有相当多的内置随机性,有时会抱怨断点的选择。