0

我有一组压力-直径数据(压力=X,直径=Y),通过以 5 到 55 的步进增量增加插管动脉的压力,然后从 55 到 5 的步进增量降低压力来测量。现在我我正在使用黄土线来最佳拟合数据。我想在一系列压力 X 下预测直径 Y:

P <- c(5.0, 10.2, 15.2, 20.0, 25.1, 30.0, 34.9, 40.1, 45.2, 50.2, 55.2, 49.9, 44.9, 40.3, 34.8, 29.8, 25.2, 20.1, 15.1,  9.8,  5.2)
D <- c(1.41, 1.47, 1.53, 1.59, 1.67, 1.74, 1.79, 1.82, 1.86, 1.89, 1.91, 1.88, 1.88, 1.85, 1.81, 1.77, 1.70, 1.63, 1.56, 1.49, 1.43)

df <- data.frame(P,D)

ggplot(df, aes(x = P, y = D)) + geom_point() + stat_smooth(method = "loess", formula = y ~ x, size = 1, se = FALSE, colour = "red")

fit <- loess(D ~ P)
xNew <- seq(5, 55, 5)  # New Pressures
predictY <- predict(fit, newdata=data.frame(y=yNew))  # Diameters Predictions
predictY
points(xNew, predictY)

但是,这会产生一个错误:“xy.coords(x, y) 中的错误:'x' 和 'y' 长度不同”因为 xNew 的长度==11 和 y 长度==21,因为测量值是上下的规模。

对于复杂的解决方法的每一行,我认为“必须有一个更好和简单的解决方案”,但看不到它。

也许 predict() 不是我需要的,我只需要使用 loess 公式来计算我感兴趣的几个点(通常是 xNew 中间的小子集) - ?最终,我只想要来自黄土拟合的数据——一个 X 序列(压力)和一个 Y 序列(直径),而不是这个双组。

任何建议将不胜感激。

谢谢!肖娜

4

2 回答 2

0

我认为您只需要预测中的向量而不是新的data.frame,

plot(df$P, df$D, col = "red")
lines(loess.smooth(P, D))
fit  <- loess(D ~ P)

xNew <- seq(5, 55, 5)  # New Pressures
predictY <- predict(fit, xNew)  # Diameters Predictions
points(xNew, predictY, col = "blue", pch = 2)

在此处输入图像描述

于 2014-03-02T22:39:27.527 回答
0

您可以ggplot使用另外一行代码来做到这一点:

(使用您的定义df

ggplot(df, aes(x = P, y = D)) + 
  geom_point() + 
  stat_smooth(method = "loess", formula = y ~ x, size = 1, se = FALSE, colour = "red")+
  geom_point(data=data.frame(P=xNew,D=predict(fit,newdata=xNew)), size=4, color="blue")

于 2014-03-02T23:47:54.310 回答