Skip to content

Instantly share code, notes, and snippets.

@naomispence
Created October 31, 2016 17:27
Show Gist options
  • Save naomispence/7b31e11c1f554dd08cadb6889dd13871 to your computer and use it in GitHub Desktop.
Save naomispence/7b31e11c1f554dd08cadb6889dd13871 to your computer and use it in GitHub Desktop.
#add these libraries
library(lsr)
library(dplyr)
#the code below limits the dataset to the 2014 survey data and creates a new dataset named GSS2014
GSS2014<-dplyr::filter(GSS, year==2014)
#get different confidence intervals for the same variable
ciMean(GSS2014$tvhours, na.rm=TRUE, conf =0.90)
ciMean(GSS2014$tvhours, na.rm=TRUE, conf =0.95)
ciMean(GSS2014$tvhours, na.rm=TRUE, conf=0.99)
#get mean for different groups with the code below
aggregate(GSS2014$tvhours, na.rm=TRUE, by=list(GSS2014$race), mean)
#Although it is not clearly labeled as such, the code below provides a 95% confidence interval
aggregate(GSS2014$tvhours, na.rm=TRUE, by=list(GSS2014$race), ciMean)
#dichotomize a variable and get confidence interval around proportion
GSS2014$nochild <-as.numeric(GSS2014$childs) <= 0
crosstab(GSS2014, row.vars = "nochild")
mean(GSS2014$nochild)
ciMean(as.numeric(GSS2014$nochild), conf =0.90)
ciMean(as.numeric(GSS2014$nochild), conf =0.95)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment