简体   繁体   中英

Specify range for numeric variable in R

I'm trying to produce a linear model from one dataset then use that linear model to predict values for another dataset. However the problem I'm getting is the predicted values are outside of the range of acceptable values. Im predicting a value between 0-30 and get values greater than 30. How do I restrict the model to this range?

A snippet of my code:

results.lm <- lm(y~a1+a2)
predict(results.lm,newdata)

My results look like this:

    1315      1316      1317 
27.271558 31.196606 24.736370

Rather than restricting your model predictions (which can't be prespecified) you can put a ceiling on the output vector afterwards:

preds <- predict(results.lm,newdata)

So try:

preds <- ifelse(preds > 30, 30, preds)

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