压力 - 直径数据预测

时间:2014-03-02 22:03:18

标签: r predict

我有一组压力 - 直径数据(压力= X,直径= Y),通过逐步增加从5到55增加空心动脉的压力,然后以55步回到5逐步增加压力来测量。现在我使用黄土线来获得最佳的数据。我想在一系列压力下预测直径Y,X:

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()不是我需要的,我只需要使用黄土公式来计算我感兴趣的几个点(通常是xNew中间的小子集) - ?最后,我想只是来自黄土的数据 - 一个X(压力)序列和一个Y(直径)序列,而不是这个双重集。

非常感谢任何建议。

谢谢! 肖纳

2 个答案:

答案 0 :(得分: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)

enter image description here

答案 1 :(得分: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")