Cheatsheet R Users ANOVA, Summaries of Design

Introduction to R. 2016-‐2017. Cheat Sheet – Analysis of Variance (2 way factorial anova, actually). Dear R learner, This is a work in progress, ...

Typology: Summaries

2021/2022

Uploaded on 08/05/2022

jacqueline_nel
jacqueline_nel 🇧🇪

4.4

(242)

3.2K documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction*to*R*******************************************************************201602017!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Cheatsheet*–*Analysis*of*Variance!
…..!!!0.!2016(2017\Cheatsheet!R!users!ANOVA.docx!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Page 1 of 3*
Introduction*to*R*
201602017*
Cheat*Sheet*–*Analysis*of*Variance*(2*way*factorial*anova,*actually)*
*
Dear*R*learner,**This*is*a*work*in*progress,*so*please*do*not*consider*this*complete.**All*suggestions*and*additions*
are*very*welcome*–*cb.*
!
Cheat!sheet!by!example!
Response!Y!=!yvar!
Factor!I!predictor!is!drug!(1=control,!2=tx,!3=other)!
Predictor!II!predictor!is!season(1=winter,!2=spring,!3=summer,!4=fall)!
!
!
Get*your*data*into*R*(3*ways)*
setwd(“/Users/cbigelow/Desktop/”)
install.packages(“openxlsx”) # Excel data (.xlsx)
library(openxlsx)
dat <- read.xlsx(“myexceldata.xlsx.dta”)
install.packages(“readstata13”)
library(readstata13)
dat <- read.dta13(“mystatadata.dta”, convert.factors=FALSE) # Stata data (.dta)
install.packages(“haven”)
library(haven)
dat <- read_sas(“mysasdata.sas7bdat”) # SAS data (.sas7bdat)
*
*
Label*factor*levels.*
levels(dat$drug) # List levels
levels(dat$drug) = c(“control”, “tx”, “other”) # Give levels names
!
!
!
Tell*R*that*the*predictors*are*FACTORS*
dat$drug <- as.factor(dat$drug)
dat$season <- as.factor(dat$season)
pf3

Partial preview of the text

Download Cheatsheet R Users ANOVA and more Summaries Design in PDF only on Docsity!

Introduction to R

Cheat Sheet – Analysis of Variance (2 way factorial anova, actually)

Dear R learner, This is a work in progress, so please do not consider this complete. All suggestions and additions

are very welcome – cb.

Cheat sheet by example

Response Y = yvar

Factor I predictor is drug (1=control, 2=tx, 3=other)

Predictor II predictor is season (1=winter, 2=spring, 3=summer, 4=fall)

Get your data into R (3 ways)

setwd(“/Users/cbigelow/Desktop/”) install.packages(“openxlsx”) # Excel data (.xlsx) library(openxlsx) dat <- read.xlsx(“myexceldata.xlsx.dta”) install.packages(“readstata13”) library(readstata13) dat <- read.dta13(“mystatadata.dta”, convert.factors=FALSE) # Stata data (.dta) install.packages(“haven”) library(haven) dat <- read_sas(“mysasdata.sas7bdat”) # SAS data (.sas7bdat)

Label factor levels.

levels(dat$drug) # List levels levels(dat$drug) = c(“control”, “tx”, “other”) # Give levels names

Tell R that the predictors are FACTORS

dat$drug <- as.factor(dat$drug) dat$season <- as.factor(dat$season)

Produce descriptives, separately for groups defined by Factor I x Factor II

require(stats) tapply(dat$yvar, list(dat$drug, dat$season), mean) # 2 way table of means (handy) install.packages(“doBy”) library(doBy) options(digits=6) summaryBy(yvar ~ drug + season, data=dat, FUN=c(length, mean, sd), fun.names=c(“n”, “mean”, “sd”)) # n, mean, sd within all groups

One way side-­‐by-­‐side box plots using ggplot( )

install.packages(“ggplot2”) library(ggplot2) blank <- ggplot(data=dat, aes(x=season, y=yvar)) # Initialize. Nothing plotted yet blank2 <- blank + labs(title=”line1\nline2”) # Note \n to obtain 2 line title points <- blank2 + geom_point( ) # Plot scatterplot pointsbox <- points + geom_boxplot(aes(color=season)) # Overlay box plot

Graph the Main Effects (not something you’d publish but handy)

plot.design(yvar ~ drug*season, data=dat, main=”Main Effects Plot”)

Graph the Interaction (also not pretty enough for publishing but handy)

install.packages(“sciplot”) library(sciplot) lineplot.CI(x.factor=drug, response=yvar, group=season, data=dat, trace.label=”Season”, xlab=”Drug”, ylab=”Mean of yvar”, main=”Interaction Plot”)

Fit 2 Way anova (main effects + interaction)

fit <- aov(yvar ~ drug*season, data=dat) # Option 1 fit <- aov(yvar ~ drug + season + drug:season, data-dat) # Option 2 (I prefer this) anova(fit) # show anova table summary(fit) # show more stuff