1
$\begingroup$

I am trying to calculate the confidence interval for a set of data with the assumption they follow Exp dist. To achieve this, I am merging this with this in R, but does not work as I am not very efficient in R.

 summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                  conf.interval=.95, .drop=TRUE) {
 library(plyr)

 # New version of length which can handle NA's: if na.rm==T, don't count them
 length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else       length(x)
 }

# This does the summary. For each group's data frame, return a vector with
 # N, mean, and sd
 datac <- ddply(data, groupvars, .drop=.drop,
             .fun = function(xx, col) {
               c(N    = length2(xx[[col]], na.rm=na.rm),
                 mean = mean   (xx[[col]], na.rm=na.rm),
                 sd   = sd     (xx[[col]], na.rm=na.rm)
               )
             },
             measurevar
)

  # Rename the "mean" column    
 datac <- rename(datac, c("mean" = measurevar))

 #   datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the 
   mean

 # Confidence interval multiplier for standard error
 # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
 # ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- qgamma(c(.025,.975), 20, 20)/ mean(datac$measurevar )


  return(datac)
 }

 mean <- 25
 dataframe <- data.frame(rnd=rexp(1000, rate = 
1/mean),Description=c(rep('A',500),rep('B',500)),dayname= c(rep( 
 'Sunday',500),rep( 'Monday',500)))

 tgc= summarySE(dataframe, measurevar="rnd", groupvars=c("Description" 
,"dayname"))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.