setwd("C:/Users/nazan/Desktop/ANLY510/Semester Long Labs Datafiles-20180511")
Lab1Part3=read.csv("Lab1Part3.csv")
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
glimpse(Lab1Part3) ###To check if variables are define correctly
## Observations: 63
## Variables: 4
## $ FocusGroup <int> 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, ...
## $ Kids <fct> alot, alot, alot, alot, alot, alot, alot, alot, alo...
## $ Animals <fct> alot, alot, alot, alot, alot, alot, alot, none, non...
## $ Perception <int> 81, 67, 77, 89, 100, 99, 65, 30, 43, 22, 15, 10, 76...
plot(density(Lab1Part3$Perception)) ###Take a look at data and see how it is distributed (looks like we might have some negative skew so lets check it with test)

library(moments)
agostino.test(Lab1Part3$Perception) ###P-value is larger than alpha==> fail to reject null hypothesis.(no sign of skew)
##
## D'Agostino skewness test
##
## data: Lab1Part3$Perception
## skew = -0.48027, z = -1.64040, p-value = 0.1009
## alternative hypothesis: data have a skewness
Checking variances(Chaeck if they are equal across factors(The test has the null hypothesis that the variances are equal and the alterntive hypothesis that they are not equal,P-values are larger than alpha==>Fail to reject null hypothesis==>Variances are equal))
Lab1Part3$FocusGroup <- factor(Lab1Part3$FocusGroup) ###Transforming FocusGroup variable from numeric to factor
bartlett.test(Lab1Part3$Perception~Lab1Part3$FocusGroup)
##
## Bartlett test of homogeneity of variances
##
## data: Lab1Part3$Perception by Lab1Part3$FocusGroup
## Bartlett's K-squared = 2.9909, df = 6, p-value = 0.81
bartlett.test(Lab1Part3$Perception~Lab1Part3$Kids)
##
## Bartlett test of homogeneity of variances
##
## data: Lab1Part3$Perception by Lab1Part3$Kids
## Bartlett's K-squared = 1.3937, df = 2, p-value = 0.4982
bartlett.test(Lab1Part3$Perception~Lab1Part3$Animals)
##
## Bartlett test of homogeneity of variances
##
## data: Lab1Part3$Perception by Lab1Part3$Animals
## Bartlett's K-squared = 4.5682, df = 2, p-value = 0.1019
Looks everythings are what we want==>Run ANOVA(including FocusGroup as a blocking factor)
model=aov(Perception~Kids*Animals+FocusGroup,data=Lab1Part3)
model
## Call:
## aov(formula = Perception ~ Kids * Animals + FocusGroup, data = Lab1Part3)
##
## Terms:
## Kids Animals FocusGroup Kids:Animals Residuals
## Sum of Squares 1152.889 20275.937 2688.825 1859.397 12222.889
## Deg. of Freedom 2 2 6 4 48
##
## Residual standard error: 15.95755
## Estimated effects may be unbalanced
summary(model) ###It seems the main effect of Animals is significant
## Df Sum Sq Mean Sq F value Pr(>F)
## Kids 2 1153 576 2.264 0.115
## Animals 2 20276 10138 39.812 6.42e-11 ***
## FocusGroup 6 2689 448 1.760 0.128
## Kids:Animals 4 1859 465 1.825 0.139
## Residuals 48 12223 255
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Making a table of findings
library(xtable)
table <- xtable(model)
table
## % latex table generated in R 3.5.0 by xtable 1.8-2 package
## % Thu Jun 28 10:45:43 2018
## \begin{table}[ht]
## \centering
## \begin{tabular}{lrrrrr}
## \hline
## & Df & Sum Sq & Mean Sq & F value & Pr($>$F) \\
## \hline
## Kids & 2 & 1152.89 & 576.44 & 2.26 & 0.1150 \\
## Animals & 2 & 20275.94 & 10137.97 & 39.81 & 0.0000 \\
## FocusGroup & 6 & 2688.83 & 448.14 & 1.76 & 0.1276 \\
## Kids:Animals & 4 & 1859.40 & 464.85 & 1.83 & 0.1393 \\
## Residuals & 48 & 12222.89 & 254.64 & & \\
## \hline
## \end{tabular}
## \end{table}
Cheking normality of residuals
qqnorm(model$residuals)

shapiro.test(model$residuals)
##
## Shapiro-Wilk normality test
##
## data: model$residuals
## W = 0.98113, p-value = 0.4444
It looks everything is good so move on ==>interpreting results==>Since only the effect of Animals is significant then we need to know which groups differ.
tapply(Lab1Part3$Perception,Lab1Part3$Animals, mean) ###alot is larger,that means has more effect.
## alot none some
## 84.80952 41.38095 68.90476
tapply(Lab1Part3$Perception,Lab1Part3$Animals, sd)
## alot none some
## 14.28852 21.76804 14.77127
TukeyHSD(model, "Animals")
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Perception ~ Kids * Animals + FocusGroup, data = Lab1Part3)
##
## $Animals
## diff lwr upr p adj
## none-alot -43.42857 -55.33867 -31.518468 0.0000000
## some-alot -15.90476 -27.81487 -3.994659 0.0062266
## some-none 27.52381 15.61371 39.433913 0.0000031
From means it seems alot has more effect and is more significant than some and none. Some also has more effect rather than none.