YDao
YDao

Reputation: 325

R: Calculate predicted mean in a lm() function

I am trying to compute the predicted/adjusted mean value of an outcome for a given simple linear regression model with both categorical and continuous variables. A quick example would be similar to the following.

dat <- data.frame(value = c(5,8,41,25,23,56,58,54,51,52,56,59),
                  x = c("A","A","A","A","A","A", "B","B","B", "B","B","G"),
                  y=c("C","C","C","D","D","D", "D","D","D", "D","E","E"), 
                  z = c(34,56,25,35,54,67,43,73,52,78,15,38))
m <- lm(value ~ x + y + z, dat)

How can I compute the adjusted mean value of the output given that x = "A", as well as the confidence interval of that value. Especially when there is another categorical variable in the model.

Thank you!!

Upvotes: 2

Views: 3999

Answers (2)

YDao
YDao

Reputation: 325

I think package lsmeans solves the question.

Upvotes: 2

code_is_entropy
code_is_entropy

Reputation: 611

I guess this is what you are looking for-

dat$predicted <- m$fitted.values
adj_mean <- aggregate(predicted ~ x, data = dat, FUN = mean)

Upvotes: 0

Related Questions