Two Factor ANOVA Errorbar plot in R

柔情痞子 提交于 2019-12-03 18:01:29

Here is another attempt using the sciplot package. Alternative ways to compute the confidence intervals can be passed in parameter ci.fun.

lineplot.CI(variable,value, group =sex.codes , data = df.2, cex = 1.5,
            xlab = "Location", ylab = "means", cex.lab = 1.2, x.leg = 1,
            col = c("blue","red"), pch = c(16,16))

I have to admit I'm quite baffled by your code. Don't take this as personal criticism, but I strongly advise you to learn your students to use the power of R as much as possible. They can only benefit from it, and my experience is that they understand faster what's going on if I don't throw lines and lines of code clutter to their heads.

First of all, you don't have to calculate the means by hand. Just do:

df.2$mean <- with(df.2,ave(value,sex.codes,variable,FUN=mean))

See also ?ave. That is more clear than the clutter of code in your example. If you have the lizard.model, you can just use

fitted(lizard.model)

and compare these values to the means.

Then I strongly disagree with you. What you calculate, is not the standard error on your prediction. To do that correctly, use the predict() function

outcome <- predict(lizard.model,se.fit=TRUE)
df.2$CI.half <- outcome$se / 2

To get the confidence interval on the predicted means, you have to use the correct formulae if you ever want your students to understand this correctly. Take a look at section 3.5 of this incredibly great Practical Regression and Anova using R from Faraway. It contains tons of code examples where everything is calculated by hand in a convenient and concise way. It will serve both you and your students. I learnt a lot from it and use it often as a guideline when explaining these things to students.

Now to get the summary dataframe you have a few options, but this one works and is quite understandible.

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 as it stands there.

Alternatively, if you want the prediction error on 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 sound harsh here and there, that's not intended. English is not my mothertongue, and I'm still not acquainted with the subtleties of the language.

[Potential shameless promotion] You should consider the functions compareCats and rxnNorm in package HandyStuff, available at www.github.com/bryanhanson/HandyStuff Warning: I'm not sure it works seamlessly with R 2.14. In particular, rxnNorm looks like the plot you are trying to produce, plus it gives you a variety of options in terms of the summarizing stats and decorations to the plot. But, this requires having your students install a separate package, so perhaps you will rule it out (but it allows students to concentrate on the presenting and analyzing the data). Plot from the ?rxnNorm example included here.

With rxnNorm you have a choice of several ways of calculating the CI's, controlled by argument "method". Here are the actual functions (from package ChemoSpec).

> seX <- function (x)  sd(x, na.rm = TRUE)/sqrt(length(na.omit(x)))
> <environment: namespace:ChemoSpec>
> 
> seXy <- function (x)  {
>     m <- mean(na.omit(x))
>     se <- seX(x)
>     u <- m + se
>     l <- m - se
>     c(y = m, ymin = l, ymax = u) } <environment: namespace:ChemoSpec>
> 
> 
> seXy95 <- function (x)  {
>     m <- mean(na.omit(x))
>     se <- seX(x)
>     u <- m + 1.96 * se
>     l <- m - 1.96 * se
>     c(y = m, ymin = l, ymax = u) } <environment: namespace:ChemoSpec>
> 
> 
> seXyIqr <- function (x)  {
>     i <- fivenum(x)
>     c(y = i[3], ymin = i[2], ymax = i[4]) } <environment: namespace:ChemoSpec>
> 
> seXyMad <- function (x)  {
>     m <- median(na.omit(x))
>     d <- mad(na.omit(x))
>     u <- m + d
>     l <- m - d
>     c(y = m, ymin = l, ymax = u) } <environment: namespace:ChemoSpec>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!