Skip to content

Instantly share code, notes, and snippets.

@aaroncharlton
Created April 15, 2015 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaroncharlton/b1ce016b7ea320423e93 to your computer and use it in GitHub Desktop.
Save aaroncharlton/b1ce016b7ea320423e93 to your computer and use it in GitHub Desktop.
# http://ww2.coastal.edu/kingw/statistics/R-tutorials/independ.html
# Study 1A
# find numbers for chi square table
indy_affect <- round(.552*29)
inter_affect <- round(.29*31)
inter_cog <- round(.71*31)
indy_cog <- round(.448*29)
# build table
tbl <- matrix(c(indy_affect,indy_cog,inter_affect,inter_cog),nrow=2)
colnames(tbl) <- c("Indie","Inter")
rownames(tbl) <- c("Affect","Cog")
tbl
library(knitr)
kable(tbl)
# chi-squared test
chisq.test(tbl) # Yate's continuity correction is the default
chisq.test(tbl, correct=FALSE) # no Yate's continuity correction
fisher.test(tbl) # fisher's exact test for count data
chisq.test(tbl, simulate.p.value=T, B=999) # Markov chain
# see what would happen if a single, solitary, individual, sole participant changed his/her mind:
chg1 <- matrix(c(-1,1,0,0), nrow=2)
chg2 <- matrix(c(0,0,1,-1),nrow=2)
tbl2 <- tbl + chg1
tbl3 <- tbl + chg2
chisq.test(tbl2, correct=FALSE)
chisq.test(tbl3, correct=FALSE)
# not enough info in 1B to look at p-curve
# a look at Experiment 2
pf(q=6.21, df1=1, df2=132, lower.tail=FALSE) # interaction self-construal and mood
pf(q=4, df1=1, df2=132, lower.tail=FALSE)
pf(q=2.33, df1=1, df2=132, lower.tail=FALSE)
# fun with study 1a
tbl
x_w_correction <- NULL
x_NO_correction <- NULL
indie_changes <- NULL
x_fisher <- NULL
x_markov <- NULL
for (i in 1:11){
indie_change <- i-6
inter_change <- 0
changes <- matrix(c(indie_change,-indie_change,inter_change,-inter_change), nrow=2)
new_tbl <- tbl + changes
indie_changes <- append(indie_changes, indie_change)
x_w_correction <- append(x_w_correction, chisq.test(new_tbl)$p.value)
x_NO_correction <- append(x_NO_correction, chisq.test(new_tbl, correct=FALSE)$p.value)
x_fisher <- append(x_fisher, fisher.test(new_tbl)$p.value)
x_markov <- append(x_markov, chisq.test(new_tbl, simulate.p.value=T, B=999)$p.value) # Markov chain
}
df <- data.frame(indie_changes, x_w_correction, x_NO_correction, x_fisher, x_markov)
library(ggplot2)
ggplot(data=df, aes(x=indie_changes)) +
geom_point(aes(y=x_NO_correction, group=1, fill="#00000")) +
geom_smooth(aes(y=x_NO_correction), se=FALSE, colour="#000000") +
geom_point(aes(y=x_w_correction, group=1, colour="#999966")) +
geom_smooth(aes(y=x_w_correction, colour="#999966"), se=FALSE) +
geom_point(aes(y=x_fisher, group=1, colour="#333333")) +
geom_smooth(aes(y=x_fisher, colour="#333333"), se=FALSE) +
geom_point(aes(y=x_markov, group=1, colour="green")) +
geom_smooth(aes(y=x_markov, colour="green"), se=FALSE) +
geom_vline(x=0) +
geom_hline(y=.05, color="red") +
geom_point(aes(x=0, y=x_NO_correction[6]), colour="red") +
ylab("P Values") +
xlab("Subjects Going the Other Way") +
ggtitle("Significance Curves: \n Changes in Independent Case") +
guides(color = "none", fill="none")
# changes in interdependent
x_w_correction <- NULL
x_NO_correction <- NULL
inter_changes <- NULL
x_fisher <- NULL
x_markov <- NULL
for (i in 1:11){
indie_change <- 0
inter_change <- i-6
changes <- matrix(c(indie_change,-indie_change,inter_change,-inter_change), nrow=2)
new_tbl <- tbl + changes
inter_changes <- append(inter_changes, inter_change)
x_w_correction <- append(x_w_correction, chisq.test(new_tbl)$p.value)
x_NO_correction <- append(x_NO_correction, chisq.test(new_tbl, correct=FALSE)$p.value)
x_fisher <- append(x_fisher, fisher.test(new_tbl)$p.value)
x_markov <- append(x_markov, chisq.test(new_tbl, simulate.p.value=T, B=999)$p.value) # Markov chain
}
df <- data.frame(indie_changes, x_w_correction, x_NO_correction, x_fisher, x_markov)
library(ggplot2)
ggplot(data=df, aes(x=inter_changes)) +
geom_point(aes(y=x_NO_correction)) +
geom_smooth(aes(y=x_NO_correction), se=FALSE) +
geom_point(aes(y=x_w_correction)) +
geom_smooth(aes(y=x_w_correction), se=FALSE) +
geom_point(aes(y=x_fisher)) +
geom_smooth(aes(y=x_fisher), se=FALSE) +
geom_point(aes(y=x_markov)) +
geom_smooth(aes(y=x_markov), se=FALSE) +
geom_vline(x=0) +
geom_hline(y=.05, color="red") +
geom_point(aes(x=0, y=x_NO_correction[6]), colour="red") +
ylab("P Values") +
xlab("Change in Preference in Interdependent Case") +
ggtitle("Significance Curves")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment