Advertisement
vanloon

summarySE R function

Dec 11th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.69 KB | None | 0 0
  1. ## Summarizes data.
  2. ## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
  3. ##   data: a data frame.
  4. ##   measurevar: the name of a column that contains the variable to be summariezed
  5. ##   groupvars: a vector containing names of columns that contain grouping variables
  6. ##   na.rm: a boolean that indicates whether to ignore NA's
  7. ##   conf.interval: the percent range of the confidence interval (default is 95%)
  8. summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
  9.                       conf.interval=.95, .drop=TRUE) {
  10.   require(plyr)
  11.  
  12.   # New version of length which can handle NA's: if na.rm==T, don't count them
  13.   length2 <- function (x, na.rm=FALSE) {
  14.     if (na.rm) sum(!is.na(x))
  15.     else       length(x)
  16.   }
  17.  
  18.   # This does the summary. For each group's data frame, return a vector with
  19.   # N, mean, and sd
  20.   datac <- ddply(data, groupvars, .drop=.drop,
  21.                  .fun = function(xx, col) {
  22.                    c(N    = length2(xx[[col]], na.rm=na.rm),
  23.                      mean = mean   (xx[[col]], na.rm=na.rm),
  24.                      sd   = sd     (xx[[col]], na.rm=na.rm)
  25.                    )
  26.                  },
  27.                  measurevar
  28.   )
  29.  
  30.   # Rename the "mean" column    
  31.   datac <- rename(datac, c("mean" = measurevar))
  32.  
  33.   datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  34.  
  35.   # Confidence interval multiplier for standard error
  36.   # Calculate t-statistic for confidence interval:
  37.   # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  38.   ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  39.   datac$ci <- datac$se * ciMult
  40.  
  41.   return(datac)
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement