質問

I have sets of Pressure-Diameter data (Pressure=X, Diameter=Y) measured by increasing the pressure of a cannulated artery in step increments from 5 to 55 and then decreasing the pressure in step increments from 55 back to 5. For now I am using a loess line for the best fit of the data. I would like to predict diameter, Y, at a sequence of pressures, 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)

However, this gives an error: "Error in xy.coords(x, y) : 'x' and 'y' lengths differ" because xNew has length==11 and y length==21 since the measurements are up and down the scale.

With each line of a convoluted work-around I think, "there must be a better and simple solution," but don't see it.

Maybe predict() is not what I need, and I just need to use the loess formula to calculate the few points in which I'm interested (typically small subset in the middle of xNew) - ? Ultimately I would like just the data from the loess fit - one sequence of X (pressures) and one sequence of Y (diameters), instead of this double set.

Any suggestions would be very appreciated.

Thanks! Shawna

役に立ちましたか?

解決

I think you just need a vector in your predict rather than the new 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

他のヒント

You can do this in ggplot with one additional line of code:

(Using your definition of 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")

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top