[R] R generated means are different from the boxplot!

Petr PIKAL petr.pikal at precheza.cz
Fri Apr 6 14:03:23 CEST 2012


Hi
> 
> Hi R-listers, 
> 
> 1) I am having trouble understanding why the means I have calculated 
from
> Aeventexhumed (A, B, and C) are different from the means showing on the
> boxplot I generated (see attached).  I have added the script as to how 
my
> data is organized. 

Maybe the difference is that boxplot show medians.

> 
> 2) Also when I went through the data manually the means I calculated for
> each nesting event are slightly different than what I generated through 
R
> (see below). 
> A,  B,  C
> 0.2155051, 0.1288241, 0.1124618

Without data it is difficult to guess. Usually when manually computed 
value is different you made manual mistake. I would hardly believe that R 
can be wrong in such a simple and extensively used function.

Regards
Petr

> 
> Thanks in advance,
> 
> Jean
> 
> -----------------------------------------------
> > require(plyr)
> Loading required package: plyr
> > resp <- read.csv(file.choose())
> > envir <- read.csv(file.choose())
> > resp <- resp[!is.na(resp$Aeventexhumed), ]
> > resp$QuadratEvent <- paste(resp$QuadratID, resp$Aeventexhumed, sep="")
> > resp$QuadratEvent <- as.character(resp$QuadratEvent)
> > envir <- envir[!is.na(envir$Aeventexhumed), ]
> > envir$QuadratEvent <- paste(envir$QuadratID, envir$Aeventexhumed, 
sep="")
> > envir$QuadratEvent <- as.character(envir$QuadratEvent)
> > ExDate <- Sector <- Quadrat <- Aeventexhumed <- NULL
> > ST1 <- ST2 <- ST3 <- ST4 <- ST0 <- NULL
> > Shells <- Hatchlings <- MaxHatch <- DeadHatch <- NULL
> > Oldeggs <- TotalEggs <- QuadratEvent <- NULL
> > for (q in unique(as.character(resp$QuadratEvent))) {
> +     s <- resp[as.character(resp$QuadratEvent) == q, ]
> +     ExDate <- c(ExDate, as.character(s$ExDate[1]))
> +     Sector <- c(Sector, as.character(s$Sector[1]))
> +     Quadrat <- c(Quadrat, as.character(s$Quadrat[1]))
> +     Aeventexhumed <- as.character(c(Aeventexhumed,
> as.character(s$Aeventexhumed[1])))
> +     QuadratEvent<- c(QuadratEvent, q)
> +     ST1 <- c(ST1, sum(s$ST1, na.rm=TRUE))
> +     ST2 <- c(ST2, sum(s$ST2, na.rm=TRUE))
> +     ST3 <- c(ST3, sum(s$ST3, na.rm=TRUE))
> +     ST4 <- c(ST4, sum(s$ST4, na.rm=TRUE))
> +     ST0 <- c(ST0, sum(s$ST0, na.rm=TRUE))
> +     Shells <- c(Shells, sum(s$Shells, na.rm=TRUE))
> +     Hatchlings <- c(Hatchlings, sum(s$Hatchlings, na.rm=TRUE))
> +     MaxHatch <- c(MaxHatch, sum(s$MaxHatch, na.rm=TRUE))
> +     DeadHatch <- c(DeadHatch, sum(s$DeadHatch, na.rm=TRUE))
> +     Oldeggs <- c(Oldeggs, sum(s$Oldeggs, na.rm=TRUE))
> +     TotalEggs <- c(TotalEggs, sum(s$TotalEggs, na.rm=TRUE))
> + }
> > responses <- data.frame(QuadratEvent, ExDate, Sector, Quadrat,
> +                         Aeventexhumed, ST0, ST1, ST2, ST3, ST4, 
Shells,
> +                         Hatchlings, MaxHatch, DeadHatch, Oldeggs,
> +                         TotalEggs, stringsAsFactors=FALSE)
> > responses$QuadratEvent <- as.character(responses$QuadratEvent)
> > data.to.analyze <- join(responses, envir, by="QuadratEvent")
> > data.to.analyze$NotHatched <- data.to.analyze$TotalEggs -
> > data.to.analyze$Shells
> > data.to.analyze$Rayos <- paste("Rayos", data.to.analyze$Rayos, 
sep=".")
> 
> > Hsuccess <- Shells/TotalEggs
> > tapply(Hsuccess, Aeventexhumed, mean, na.rm=TRUE)
>         A         B         C 
> 0.2156265 0.1288559 0.1124327 
> > boxplot(HSuccess ~ Aeventexhumed, data = data.to.analyze, col = 
"blue",
> +         main = "Hatching Success of Arribadas in 2010",
> +         xlab = "Arribada Event",
> +         ylab = "Hatching Success % (Shells / Total Eggs)")
> 
> http://r.789695.n4.nabble.com/file/n4536926/hatch_Aeventexhumed.png 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/R-generated-
> means-are-different-from-the-boxplot-tp4536926p4536926.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list