1

I have data that looks like this:

height <- c(1,2,3,4,2,4,6,8)
weight <- c(12,13,14,15,22,23,24,25)
type <- c("Wheat","Wheat","Wheat","Wheat","Rice","Rice","Rice","Rice")
set <- c(1,1,1,1,2,2,2,2)
dat <- data.frame(set,type,height,weight)

I run a lmer model with set as a random effect in R:

mod <- lmer(weight~height + type + (1|set), data = dat)

Now, I want to plot the estimates of the model and plot a regression, with weight on the x-axis and height on the y-axis, facet(~type)

I use the predict function as follows

dat$pred <- predict(mod, type = "response")

And I want to achieve a ggplot that will look like this:

ggplot(dat,aes(x = weight, y = height)) +
geom_point() + geom_smooth(method="lm", fill=NA) + facet_grid(~ type, scales = "free") 

However, I note that the predict function has only a singular output. How do I plot that to achieve the same as above? Or do I have to store two different predict responses, and then plug it into the x,y of ggplot?

1 Answer 1

1

I can adapt your plot to show raw vs. predicted values like this:

ggplot(dat,aes(y = height)) +
    geom_point(aes(x = weight)) +
    geom_line(aes(x = pred)) + 
    facet_grid(~ type, scales = "free")

In your example plot though you have weight, the outcome variable in your model, on the x-axis, which is confusing. Normally you would have the outcome/predicted variable on the y-axis, so I would have plotted your model predictions like:

ggplot(dat,aes(x = height)) +
    geom_point(aes(y = weight)) +
    geom_line(aes(y = pred)) + 
    facet_grid(~ type, scales = "free")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.