为什么 R 分段包的预测函数 return 出错?

Why does R segmented package's predict function return an error?

我正在使用 segmented 包创建带断点的线性模型,但是当我使用 predict 函数时 returns 出现以下错误:

Error in matrix(x.values, nrow = n, ncol = k, byrow = FALSE) : 
 'data' must be of a vector type, was 'NULL'

这是我的数据:

 > dput(spring.plot)
structure(list(Year = c(1995, 1996, 1997, 1998, 1999, 2000, 2001, 
2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012, 2014, 
2017, 2018), cor.d13C.x = c(-20.1731091673535, -19.9205871049139, 
-20.6374520869892, -20.1851878984857, -20.4450023031073, -20.7036033075104, 
-21.0707619356372, -20.7402193699317, -21.54775822157, -21.1569885512026, 
-21.3501796817414, -21.6191130116038, -21.6492696573744, -21.6213435694402, 
-21.5670838470492, -21.6346790950157, -22.1278529495184, -22.0151931682557, 
-22.1203815975813, -21.8421435334785), cor.d13C.y = c(0.187648385511187, 
0.303333143562945, 0.322078617191699, 0.216439794978181, 0.183331753701332, 
0.444176460919643, 0.287401841353699, 0.268731238031834, 0.18822947285027, 
0.133288158421617, 0.264489364220891, 0.443636742676881, 0.228538121333274, 
0.326033302668887, 0.396003654535805, 0.272766639830925, 0.549447170011753, 
0.245148399918538, 0.378003453281401, 0.31552762151098), d15N.x = c(9.2105, 
9.636, 9.7571, 10.1341111111111, 9.8055, 10.06425, 9.8555, 10.4192, 
9.8879, 10.5179, 10.545625, 10.3765, 10.5826, 10.3744444444444, 
10.2484, 9.5749, 9.52, 9.873, 10.0519, 10.8419), d15N.y = c(0.357452172402904, 
0.418821096783712, 0.286839269742946, 0.367195262920304, 0.312860725137134, 
0.244967490554505, 0.324268150352965, 0.459335703912606, 0.502120049833149, 
0.264608914773818, 0.202960895248321, 0.499248379511085, 0.590404607028096, 
0.471943882021769, 0.448001289680683, 0.278745383618974, 0.552719941139573, 
0.484833734387367, 0.547101341414712, 0.252805348923721), PerN.x = c(12.9007502555847, 
12.8167667388916, 12.6170592308044, 12.6319534513685, 13.5568219184875, 
12.5970660448074, 12.5292349815369, 12.3908371925354, 12.8273427963257, 
12.3977887153625, 13.0017735958099, 12.3305891036987, 12.5119565963745, 
13.4092265235053, 12.9288623809814, 13.1139984130859, 12.9217474937439, 
12.8078309165107, 13.5686268806458, 13.0825483322144), PerN.y = c(0.561407252949615, 
0.589777982603924, 0.403203366840876, 0.696942305294126, 0.34608685502915, 
0.455015723273848, 0.636465123996367, 0.556050554528199, 0.469391267667275, 
0.343232155115455, 0.45609833258894, 0.430321356385325, 0.540299777960146, 
0.528607495036965, 0.603123576500966, 0.549651250473174, 0.384079375553638, 
0.51738099428862, 0.365028403333915, 0.361264153892095), PerC.x = c(46.6922031402588, 
45.7764064788818, 46.4129936218262, 46.5377320183648, 44.9868389129639, 
43.2995181083679, 45.2353702545166, 45.0508312225342, 45.2698936462402, 
42.8951564788818, 43.2648825645447, 43.9498802185059, 44.9562057495117, 
45.7544178432888, 45.4925720214844, 44.8370536804199, 44.1818626403809, 
45.5411309136285, 45.9677772521973, 47.623962020874), PerC.y = c(0.89834948737305, 
1.15055827353951, 1.07068229762836, 1.55762241960512, 0.660824558535851, 
1.49350374350045, 1.30504666418671, 1.22412934748672, 0.899609693326907, 
0.905523389594839, 1.15335080765026, 1.33751682282367, 1.32474641315414, 
1.60474345054736, 0.972492166400552, 0.919991354863598, 1.24135231820059, 
1.57815525005946, 0.997063472019759, 1.15715655760894), n = c(10L, 
10L, 10L, 9L, 10L, 8L, 10L, 10L, 10L, 10L, 8L, 10L, 10L, 9L, 
10L, 10L, 10L, 9L, 10L, 10L)), row.names = c(NA, -20L), class = "data.frame")

还有我的代码:

my.lm.n.s <- lm(d15N.x ~ Year, data = spring.plot)
summary(my.lm.n.s)

my.seg.2.n.s <- selgmented(my.lm.n.s, seg.Z = ~ Year , alpha = 0.05, type = c("bic"),
                         return.fit = TRUE, bonferroni = TRUE, Kmax = 2, msg=TRUE)
my.seg.2.n.s

seg.n.spring <- segmented(my.lm.n.s, seg.Z = ~ Year, npsi = 2)
seg.n.spring

new <- data.frame(c(min(spring.plot$Year), seg.n.spring$psi[, "Est."], max(spring.plot$Year)))
lines <- predict(seg.n.spring, new)

new 对象是一个数据框,所以我不知道为什么它不起作用。

我的问题与this one非常相似。

您必须更新一行代码才能使其正常工作。

new <- data.frame(Year = c(min(spring.plot$Year), seg.n.spring$psi[, "Est."], max(spring.plot$Year)))

所以 data.frame 需要一个正确的列名。