简体   繁体   中英

R linear regression- interpolation - approx- xout gave in NA

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.

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)))

答案:近似函数 - 总是计算给定的“x 值”的“y 值”

 approx(x = dat.model$fitted.values, y =stddat$x, xout = calForx)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM