KBoehme
KBoehme

Reputation: 401

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

Upvotes: 0

Views: 604

Answers (1)

Gary Weissman
Gary Weissman

Reputation: 3627

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)

Upvotes: 2

Related Questions