menu

1.9 - Summary of important R code

by

The main components of R code used in this chapter follow with components to modify in red, remembering that any R packages mentioned need to be installed and loaded for this code to have a chance of working:

  • summary(DATASETNAME)
    • ◦ Provides numerical summaries of all variables in the data set.
  • t.test(Y~X,data=DATASETNAME,conf.level=0.95)
    • ◦ Provides two-sample t-test test statistic, df, p-value, and 95% confidence interval.
  • 2*pt(abs(Tobs),df=DF,lower.tail=F)
    • ◦ Finds the two-sided test p-value for an observed 2-sample t-test statistic of Tobs.
  • hist(DATASETNAME$Y)
    • ◦ Makes a histogram of a variable named Y from the data set of interest.
  • boxplot(Y~X,data=DATASETNAME)
    • ◦ Makes a boxplot of a variable named Y for groups in X from the data set.
  • beanplot(Y~X,data=DATASETNAME)
    • ◦ Makes a beanplot of a variable named Y for groups in X from the data set.
    • ◦ Requires the beanplot package is loaded.
  • mean(Y~X,data=DATASETNAME); sd(Y~X,data=DATASETNAME)
    • ◦ Provides the mean and sd of responses of Y for each group described in X.
  • favstats(Y~X,data=DATASETNAME)
    • ◦ Provides numerical summaries of Y by groups described in X.
  • Tobs <- t.test(Y~X,data=DATASETNAME,var.equal=T)$statistic; Tobs

    B<-1000

    Tstar<-matrix(NA,nrow=B)

    for (b in (1:B)){

    Tstar[b]<-t.test(Y~shuffle(X),data=DATASETNAME,var.equal=T)$statistic

    }

    • ◦ Code to run a for loop to generate 1000 permuted versions of the test statistic using the shuffle function and keep track of the results in Tstar.
  • pdata(abs(Tobs),Tstar,lower.tail=F)
    • ◦ Finds the proportion of the permuted test statistics in Tstar that are less than -|Tobs| or greater than |Tobs|,
  • Tobs <- compareMeans(Y~X,data= DATASETNAME); Tobs

    B<-1000

    Tstar<-matrix(NA,nrow=B)

    for (b in (1:B)){

    Tstar[b]<-compareMeans(Y~X,data=resample(DATASETNAME))

    }

    • ◦ Code to run a for loop to generate 1000 bootstrapped versions of the data set using the resample function and keep track of the results of the statistic in Tstar.
  • qdata(c(0.025,0.975),Tstar)
    • ◦ Provides the values that delineate the middle 95% of the results in the bootstrap distribution (Tstar).