简体   繁体   中英

How to find min max from lm

I'm trying to figure out a way to find the minimum/maximum from a fitted quadratic model. In this case the minimum.

x.lm <- lm(Y ~ X + I(X^2))

Edit: To clarify, I can already find the minimum y through min(predict(x.lm)). How can I translate this to it's corresponding x value.

Check this out. Idea is that you have to take fitted values form 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)

After another question

df <- cbind(X, Y, fits)
df <- as.data.frame(df)
index <- which.min(df$fits) #very usefull command
row.in.df <- df[index,]

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