Mean and Confidence interval for Groups of Variable in R

Patrick picture Patrick · Feb 5, 2018 · Viewed 8k times · Source

I'm new to R and I'm attempting to do something that I believe should be very simple but code online has not helped.

data <- structure(list(Group = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), 
Time = c(1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2), mean_PctPasses = c(68.26, 
60.2666666666667, 62.05, 66.3833333333333, 59.7333333333333, 
69.7714285714286, 57.1888888888889, 63.8875, 61.1833333333333, 
59.775, 66.2666666666667, 62.12), mean_AvgPassing = c(7.3, 
7.01111111111111, 6.35, 9.26666666666667, 6.68333333333333, 
8.78571428571429, 5.87777777777778, 8.3125, 7.63333333333333, 
7.7, 8.38333333333334, 6.89), mean_AvgRush = c(0.3, -0.3, 
3.5, 0.75, 5, 1.47142857142857, 5.71111111111111, 3.3875, 
2.74, 6.6, 4.5, 5), mean_Int = c(0.2, 0.777777777777778, 
0.25, 0.5, 1.5, 0.857142857142857, 0.777777777777778, 0.75, 
0.666666666666667, 0.75, 0.833333333333333, 1.1), mean_Rate = c(99.3, 
88.5222222222222, 80.5, 106.45, 77.2333333333333, 102.885714285714, 
76.8888888888889, 100.075, 92.1166666666667, 78.55, 98.05, 
79.56)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-12L), .Names = c("Group", "Time", "mean_PctPasses", "mean_AvgPassing", 
"mean_AvgRush", "mean_Int", "mean_Rate"))

Using this dataset I have 2 grouping variables "Group" and "Time". I would like to get the means and confidence intervals in a table format for each of these combinations for the variables mean_PctPasses to mean_Rate and save the result so it is in a table. I need it to be in a table because I will refer to it later in plotly. Doing this in SPSS is quite easy.

I've attempted several functions and below is issues I've had with each

library(rcompanion)    
ci.mean(mean_PctPasses~Group+Time, data = data)

library(DescTools)
MeanCI(data$mean_PctPasses)

library(Rmisc)
CI(data$mean_PctPasses,    ci=0.95)

MeanCI, ci.mean and CI don't allow for multiple variables to be listed and are saved as a table (only shows up in console)

by(data = data, data$Group, FUN = stat.desc)

This won't allow me to group my data according to both Group and Time. Below is an example of the graph I hope to build in R (shown in SPSS).

SPSS Example

Any help/assistance on this would be great. Let me know if any clarifications are needed and I'll be sure to edit my initial post.

UPDATE

After some great answers (thank you Rob and Steven) I felt I needed to clarify my question a little bit. I would like to get statistics for each group (not individually) for all statistics (mean_PctPasses to mean_Rate). An example of a function that produces the stats I would like for one variable is shown below using Rmisc library(Rmisc) group.UCL(mean_PctPasses~Group+Time , data, FUN=CI) This gives me the following output only for mean_PctPasses Output Using Rmisc

But what I would like to have is the following (which I have photoshopped) Image of Desired Ouput

This could, of course, be shown in the other orientation (example below with SPSS and SEM). Alternative Orientation example in SPSS

Answer

Steven picture Steven · Feb 5, 2018

Doing this with R is easy, too.

Another way, using CI() from Rmisc:

library(dplyr)
library(Rmisc)
library(ggplot2)

data <- 
  data %>%
  group_by(Group, Time) %>%
  dplyr::summarise(avg_PctPasses = mean(mean_PctPasses), 
            uci_PctPasses = CI(mean_PctPasses)[1], 
            lci_PctPasses = CI(mean_PctPasses)[3]) %>%
  mutate(Time = Time %>% as.factor())

Admittedly, I'm not a big fan of the "magic numbers" after the call to CI().

Plotting the data is equally simple.

data %>%
  ggplot(aes(x = Group, y = avg_PctPasses, fill = Time)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(ymin = lci_PctPasses, ymax = uci_PctPasses), position = "dodge")

enter image description here