我正试图找出一种从拟合二次模型中找到最小值/最大值的方法。在这种情况下最小。
x.lm <- lm(Y ~ X + I(X^2))
编辑:为了澄清,我已经找到了最小的y到min(预测(x.lm))。如何将其转换为相应的x值。
答案 0 :(得分:2)
检查一下。想法是你必须从x.lm fit
获取拟合值#example data
X <- 1:100
Y <- 1:100 + rnorm(n = 100, mean = 0, sd = 4)
x.lm <- lm(Y ~ X + I(X^2))
fits <- x.lm$fitted.values #getting fits, you can take residuals,
# and other parameters too
# I guess you are looking for this.
min.fit = min(fits)
max.fit = max(fits)
df <- cbind(X, Y, fits)
df <- as.data.frame(df)
index <- which.min(df$fits) #very usefull command
row.in.df <- df[index,]