setwd("C:/Users/lhomm/OneDrive/Documents/R")
Dat<- read.table("C:/Users/lhomm/OneDrive/Documents/R/Design2 Data.txt",header=FALSE)
colnames(Dat) = c("Offer", "Age_Group", "Count")
head(Dat)
## Offer Age_Group Count
## 1 23 Young 1
## 2 25 Young 2
## 3 21 Young 3
## 4 22 Young 4
## 5 21 Young 5
## 6 22 Young 6
Dat$Age_Group = factor(Dat$Age_Group)
boxplot(Offer ~ Age_Group, data = Dat, col = "white", xlab = " ")
stripchart(Offer ~ Age_Group, data = Dat, method = "jitter", vertical = T, pch = 1, cex = 1.5, ylab = "Offer", main = "Age Group", col = c("red", "blue", "green"), add=TRUE)

### The factor level means according to the dot/box and whisker plot are different. It seems that the means for youth and elderly are similar but they are both very different from the factor mean of the middle age respondents. ###
Dat_ANOVA <- aov(Offer ~ Age_Group, data = Dat)
summary(Dat_ANOVA)
## Df Sum Sq Mean Sq F value Pr(>F)
## Age_Group 2 316.7 158.36 63.6 4.77e-12 ***
## Residuals 33 82.2 2.49
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Dat_ANOVA$fitted.values
## 1 2 3 4 5 6 7 8
## 21.50000 21.50000 21.50000 21.50000 21.50000 21.50000 21.50000 21.50000
## 9 10 11 12 13 14 15 16
## 21.50000 21.50000 21.50000 21.50000 27.75000 27.75000 27.75000 27.75000
## 17 18 19 20 21 22 23 24
## 27.75000 27.75000 27.75000 27.75000 27.75000 27.75000 27.75000 27.75000
## 25 26 27 28 29 30 31 32
## 21.41667 21.41667 21.41667 21.41667 21.41667 21.41667 21.41667 21.41667
## 33 34 35 36
## 21.41667 21.41667 21.41667 21.41667
Fitted_Value_Means <- tapply(Dat$Offer, Dat$Age_Group, mean)
Fitted_Value_Means
## Elderly Middle Young
## 21.41667 27.75000 21.50000
### yˆ1i = y¯1 = 21.5, yˆ2i = y¯2 = 27.75, yˆ3i = y¯3 = 21.416 ###
Dat_ANOVA$residuals
## 1 2 3 4 5 6 7
## 1.5000000 3.5000000 -0.5000000 0.5000000 -0.5000000 0.5000000 -1.5000000
## 8 9 10 11 12 13 14
## 1.5000000 -2.5000000 0.5000000 -2.5000000 -0.5000000 0.2500000 -0.7500000
## 15 16 17 18 19 20 21
## -0.7500000 1.2500000 -1.7500000 1.2500000 -0.7500000 2.2500000 0.2500000
## 22 23 24 25 26 27 28
## -0.7500000 -1.7500000 1.2500000 1.5833333 -1.4166667 3.5833333 -0.4166667
## 29 30 31 32 33 34 35
## 0.5833333 1.5833333 -0.4166667 -1.4166667 -2.4166667 -1.4166667 0.5833333
## 36
## -0.4166667
### F-Test for equality of means ###
### Ho: All factor means are equal. H1: At least one factor mean is different. ###
### We reject at alpha = .01 ###
Decsion_Rule <- qf(0.01, 2, 33, lower.tail=FALSE) ### Decision Rule ###
Decsion_Rule
## [1] 5.312029
Dat_ANOVA2 <- anova(lm(Offer ~ Age_Group, data = Dat))
Dat_ANOVA2$`F value`
## [1] 63.60142 NA
Dat_ANOVA2$`Pr(>F)`
## [1] 4.768937e-12 NA
### With a high F-observed value of 63.6 being larger than 5.31 and a low of P-value 4.768937e-12 we reject the null hypothesis at alpha = .01 ###
### There seems to be a quadratic relationship between age group and mean cash offer for a car. Young people and elderly people both received similarly low mean cash offers while middle aged people received higher mean cash offers. So cash offers seem to increase with age and then decline. But we need to do t-tests to determine the difference in means between each individual age-group. ###
alpha.1 <- c(-3,-2,1,3,1)
PowerTest <- power.anova.test(groups = length(alpha.1),
between.var = var(alpha.1),
within.var = 10^2, power = .95)
PowerTest
##
## Balanced one-way analysis of variance power calculation
##
## groups = 5
## n = 78.33752
## between.var = 6
## within.var = 100
## sig.level = 0.05
## power = 0.95
##
## NOTE: n is number in each group
ceiling(PowerTest$n)
## [1] 79
### The sample size n for each group needed to achieve a power of .95 is ~78 or 79 but to be conservative we will stick with a minimum of 79. ###