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