Reputation: 11
I want to predict values based on the standard graph- below is data and code for linear regression and interpolation
Library(tidyverse)
stddat = tibble(x = c (5, 25, 50, 125, 250),
y = c(.0173, 0.123, 0.242, 0.545, 0.958))
plot(x = stddat$x, y = stddat$y)
dat.model = lm(stddat$y~stddat$x)
result_values = approx(y = dat.model$fitted.values, x =stddat$x)
lines(x = result_values$x, y = result_values$y)
calForx = c("B_ad" = 0.662, "A_ad" = 0.0091)
approx(y = dat.model$fitted.values, x =stddat$x, xout = calForx)
But output was
$x
Before_adsorption After_adsorption
0.6620 0.0091
$y
[1] NA NA
what is wrong? How to predict the "x values" for given "y values". Please provide information for getting the results.
Upvotes: 0
Views: 492
Reputation: 11
Answer: Approx function-always calculates "y values" for-given "x values"
approx(x = dat.model$fitted.values, y =stddat$x, xout = calForx)
Upvotes: 1
Reputation: 3388
Use predict
to predict new values from the model.
# Pass stddat to lm so it knows the column name of the independent variable
dat.model = lm(y~x, data=stddat)
# Predict y for new values of x
predict(dat.model, newdata=data.frame(x=c(0.662, 0.0091)))
Upvotes: 1