You are on page 1of 2

R CODE FOR ANOVA

# one way ANOVA

## input data

Factory <- c(rep('F1',5), rep('F2',6), rep('F3',5))

Productivity <- c(8,6,9,8,7,9,6,9,7,6,8,5,6,5,7,6)

## one way ANOVA

one.factor <- aov(Productivity ~ Factory)

summary(one.factor)

# pair means test

## Bonferroni test

pairwise.t.test(Productivity, Factory, p.adjust.method = "bonferroni")

## Holm test

pairwise.t.test(Productivity, Factory)

## TukeyHSD

TukeyHSD(one.factor)

# To run Levene test, we need to install package “car”

install.packages("car")

library(car)

# Homogeneity of Variances test

leveneTest(Productivity ~ Factory, center = mean)

# Two way ANOVA

## input data

Adv <- c(rep('h',6),rep('m',6),rep('l',6),rep('n',6))

z1 <- c('i','i','s','s','o','o')

Area <- c(rep(z1,4))

Sale <- c(12,14,11,12,10,11,10,9,9,7,8,8,9,10,5,6,7,6,6,5,7,6,5,5)

# Two ways ANOVA with interaction

two.factor.inter <- aov(Sale ~ Adv * Area)


summary(two.factor.inter)

## Two ways ANOVA without interaction

two.factor <- aov(Sale ~ Adv + Area)

summary(two.factor)

## Example 3.3

Profit <- c(8, 7, 6, 5, 4, 6, 7, 5, 3, 6, 5, 8, 7, 5, 5, 4, 4, 3)

Season <- c(rep('S',9),rep('W',9))

Scale <- c(rep('B',3),rep('M',4), rep('S',2),rep('B',3),rep('M',4), rep('S',2))

## Two ways ANOVA without interaction

two.factor <- aov(Profit ~ Season + Scale, data = mydata)

summary(two.factor)

## Two ways ANOVA with interaction

two.factor.inter <- aov(Profit ~ Season * Scale)

summary(two.factor.inter)

# To run Levene test for two way ANOVA, we need to install package “car” and data should be in data
frame.

mydata <- data.frame(Profit, Season, Scale)

library(car)

leveneTest(Profit ~ Season + Scale, data = mydata)

leveneTest(Profit ~ Season + Scale, data = mydata, center = mean)

leveneTest(Profit ~ Season*Scale, data = mydata)

leveneTest(Profit ~ Season*Scale, data = mydata, center = mean)

You might also like