John
John

Reputation: 1947

How to predict single value using function fitted()?

Let's consider model following :

set.seed(42)
y <- rnorm(100)
x <- data.frame('Exp' = rexp(100),'Poiss' = rpois(100,1))
model_1 <- lm(y~., data = x)

I want to predict value for model_1 of (Exp = 1, Poiss = 0.5).

To do this I used code : fitted(model_1, data.frame(Exp = 1, Poiss =0.5)).

However I get 100 values instead of one predicted. What I'm doing wrong ?

Upvotes: 0

Views: 114

Answers (1)

Duck
Duck

Reputation: 39595

You can use:

#Code
predict(model_1, data.frame(Exp = 1, Poiss =0.5))

Because fitted() does this:

fitted is a generic function which extracts fitted values from objects returned by modeling functions. fitted.values is an alias for it.

That is why you are getting 100 values, as this is extracting the data fitted from the original model_1.

Upvotes: 1

Related Questions