I have to admit I'm pretty puzzled by your code. Do not take this as personal criticism, but I highly recommend that you teach your students how to use the R power as much as possible. They can benefit from it, and my experience is that they quickly understand what happens if I don't throw lines and lines of code clutter in their heads.
First of all, you do not need to calculate funds manually. Just do:
df.2$mean <- with(df.2,ave(value,sex.codes,variable,FUN=mean))
See also ?ave This is more understandable than the mess of code in your example. If you have lizard.model you can just use
fitted(lizard.model)
and compare these values ββwith the means.
Then I strongly disagree with you. What you calculate is not a standard error in your prediction. To do this correctly, use the predict() function
outcome <- predict(lizard.model,se.fit=TRUE) df.2$CI.half <- outcome$se / 2
To get the confidence interval for predicted funds, you must use the correct formulas if you want your students to get it right. Take a look at section 3.5 of this incredibly large Practical Regression and Anova using R from Faraway. It contains many code examples, where everything is calculated manually in a convenient and concise way. He will serve you and your students. I learned a lot from him and often used it as a guide when explaining these things to students.
Now, to get a summary data file, you have several options, but this one works and is understandable.
summary.df <- unique(df.2[,-c(3,5,6)]) names(summary.df) <- c('Sex','Location','Means','CI.half')
And now you can just run your plot code while it is standing.
Alternatively, if you want to get a prediction error for your values, you can use the following:
lizard.predict <- predict(lizard.model,interval='prediction') df.2$lower <- lizard.predict[,2] df.2$upper <- lizard.predict[,3] summary.df <- unique(df.2[,-3]) names(summary.df)[1:3] <- c('Sex','Location','Means') qplot(data=summary.df, y=Means, x=Location, group=Sex, ymin=lower, ymax=upper, geom=c("point", "errorbar", "line"), color=Sex, shape=Sex, width=0.25) + theme_bw()
PS: If I feel harsh here and there, this is not intended. English is not my native language, and I'm still not familiar with the intricacies of the language.