Home‎ > ‎Schedule‎ > ‎

R code for April 26


###
# ONE FACTOR REPEATED MEASURES ANOVA
####

hinton.keyboard.altformat = data.frame(errors=c(5,1,0,2,6,2,4,4,10,3,5,6),kb=c("one","one","one","one","two","two",
"two","two","three","three","three","three"),part=c("a","b","c","d","a","b","c","d",
"a","b","c","d"))

bwplot(errors ~ as.factor(kb),data=hinton.keyboard.altformat)
bwplot(errors ~ as.factor(kb)|part,data=hinton.keyboard.altformat)

#Again we'll use a slightly different format to work through this

hinton.keyboard = data.frame(
kb1=c(5,1,0,2),kb2=c(6,2,4,4),kb3=c(10,3,5,6)
)

numberofdatapoints = 12
numberofconditions = 3
numberofparticipants = 4

df_total = numberofdatapoints - 1
df_between = numberofconditions - 1
df_err = (numberofparticipants-1)*(numberofconditions-1)

SS_total = sum((hinton.keyboard - (sum(hinton.keyboard)/numberofdatapoints))^2)

SS_between_conditions = sum((mean(hinton.keyboard) - (sum(hinton.keyboard)/numberofdatapoints))^2*numberofparticipants)

SS_within_conditions = SS_total - SS_between_conditions 

SS_between_subjs = sum((rowMeans(hinton.keyboard) - mean(mean(hinton.keyboard)))^2)*numberofconditions

SS_error = SS_within_conditions - SS_between_subjs

MS_between_conditions= SS_between_conditions/df_between

MS_error = SS_error/df_err

F = MS_between_conditions/MS_error

1 - pf(F,df_between,df_err)

# We can do this without all of the steps...

kb.aov = aov(errors ~ kb + Error(part/kb),data=hinton.keyboard.altformat)

#And to relate this back to regression again....The following command gives us the 
# same thing!

anova(lm(errors ~ part,data=hinton.keyboard.altformat),lm(errors ~ part + kb,data=hinton.keyboard.altformat))

#And again we need additional tests to locate the differences..

pairwise.t.test(hinton.keyboard.altformat$errors,hinton.keyboard.altformat$kb,paired=T,
p.adj="bonferroni")

# Tests of contingency


passive.data <- matrix(c(17, 5, 11, 16), nr=2, dimnames=list(c("Active", "Passive"), c("Correct", "Incorrect")))



# A chi-squared test of contingency


chisq.test(passive.data)


#If count in any cell is < 5:


fisher.test(passive.data)



# Computing the distance between vectors
vec1 = c(1,2,3,4)

vec2 = c(4,2,5,1)
dist(rbind(vec1, vec2))

euclidean = function(v1, v2) {
  sqrt(sum((v1 - v2)**2))
}

euclidean( vec1, vec2)

# Euclidean distance is Minkowski distance with p=2
dist(rbind(vec1, vec2), method = "minkowski", p = 2)

# But there are other Minkowski distances
dist(rbind(vec1, vec2), method = "minkowski", p = 3)

# cosine similarity (opposite of a distance measure!)
cosine = function(v1, v2) {
  denominator = sqrt(sum(v1**2)) * sqrt(sum(v2**2))
  numerator = sum(v1 * v2)
  numerator / denominator
}

cosine(vec1, vec2)

Comments