Load packages and data into R

library("ggplot2")
library("reshape2")
library("stringr")
library("plyr")
library("dplyr")
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:plyr':
## 
##     arrange, desc, failwith, id, mutate, summarise, summarize
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
setwd("C:/Users/World/Desktop/Joe/Research/MDM - Rutgers/Partitioning Study/MTurk")
Pdata <- read.csv("partition default svo data EDIT.csv")

Calculate the mean of svo_angle and make new column that has svo_angle centered to the mean

mean <- mean(Pdata[,4])

Pdata$svo_mean <- Pdata$svo_angle - mean

Now dichotomize the consume (not cooperate is 1 and cooperate is 0)

Pdata$coop <- ifelse(Pdata$consume > 10, 1, 0)

Some graphs (-1 is no partition, 1 is partition)

Coop.1 <- subset(Pdata, Pdata$coop == 0)

frequency1 <- ddply(Coop.1, .(partition), summarize, length=length(coop))

count(Pdata$partition==-1)
##       x freq
## 1 FALSE  252
## 2  TRUE  253
frequency1$percentage <- ifelse(frequency1$partition==-1, frequency1$length / 253, frequency1$length / 252) * 100

frequency1$partition <- ifelse(frequency1$partition==-1, "No Partition", "Partition")

ggplot(frequency1, aes(y=length, x=factor(partition))) + 
  geom_bar(stat="identity", position="dodge", fill="salmon") + 
  scale_y_continuous(limits=c(0,200), breaks=seq(0,200,40)) + 
  ggtitle("Number of people that cooperated by partition") + 
  xlab("Partition") + ylab("# of people that cooperated") + 
  geom_path(x=c(1,1), y=c(185,190)) + 
  geom_path(x=c(1,2), y=c(190,190)) +
  geom_path(x=c(2,2), y=c(185,190)) + 
  annotate("text",x=1.5,y=195,label="***")

plot of chunk unnamed-chunk-4

ggplot(frequency1, aes(y=percentage, x=factor(partition))) + 
  geom_bar(stat="identity", position="dodge", fill="salmon") + 
  scale_y_continuous(limits=c(0,100), breaks=seq(0,100,20)) + 
  ggtitle("Percentage of people that cooperated by partition") + 
  xlab("Partition") + ylab("% of people that cooperated") + 
  geom_path(x=c(1,1), y=c(70,75)) + 
  geom_path(x=c(1,2), y=c(75,75)) + 
  geom_path(x=c(2,2), y=c(75,70)) + 
  annotate("text",x=1.5,y=80,label="***")

plot of chunk unnamed-chunk-4

twobytwo <- ddply(Pdata, .(partition, coop), summarize, length = length(coop))

twobytwo$percentage <- (twobytwo$length/505) * 100

ggplot(twobytwo, aes(y = percentage, x = factor(partition), fill = factor(coop))) + 
    geom_bar(stat = "identity", position = "dodge") + 
    scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, 20)) + 
    ggtitle("Percentage of people that cooperated by partition and cooperation") + 
    xlab("Partition") + 
    ylab("% of people that cooperated") + 
    scale_fill_discrete(name = "Cooperation", breaks = c("1", "0"), labels = c("No cooperation", "Cooperation"))

plot of chunk unnamed-chunk-4

ggplot(Coop.1, aes(x=svo_mean)) + 
  geom_step(stat="ecdf", color="salmon") + 
  ggtitle("Cumulative density distribution for svo_mean for people that cooperated")

plot of chunk unnamed-chunk-4

ggplot(Pdata, aes(x=svo_mean, colour=factor(coop))) + 
  geom_step(stat="ecdf") + 
  ggtitle("Cumulative density distribution for \n cooperation vs no cooperation") + 
  scale_colour_discrete(name="Cooperation", breaks=c("1", "0"), labels=c("No cooperation", "Cooperation")) + 
  theme(plot.title = element_text(lineheight=.8, face="bold")) + 
  annotate("text",x=0,y=.8,label="***")

plot of chunk unnamed-chunk-4

mean.df <- ddply(Pdata, .(coop), summarize, mean=mean(svo_mean))

ggplot(mean.df, aes(x=factor(coop), y=mean)) + 
  geom_bar(stat="identity", position="dodge", fill="salmon") + 
  scale_y_continuous(limits=c(-.1,.1), breaks=seq(-.1,.1,.02)) + 
  geom_path(x=c(1,1), y=c(.08,.09)) + 
  geom_path(x=c(1,2), y=c(.09,.09)) + 
  geom_path(x=c(2,2), y=c(.09,.08)) + 
  annotate("text", x=1.5,y=.095, label="***") + 
  ggtitle("Mean SVO score for cooparation(0) and \n no cooperation(1)") + 
  xlab("Cooperation") + 
  ylab("mean svo score") + 
  theme(plot.title=element_text(face="bold"))

plot of chunk unnamed-chunk-4

After looking at some the dichotomized data we will now graph the continuous variable data

mean_consume <- ddply(Pdata, .(partition), summarize, mean.c=mean(consume))

ggplot(mean_consume, aes(x=factor(partition), y=mean.c)) + 
  geom_bar(stat="identity", position="dodge", fill="salmon") + 
  scale_y_continuous(limits=c(0, 40), breaks=seq(0,40,10)) + 
  geom_path(x=c(1,1), y=c(30,32)) + 
  geom_path(x=c(1,2), y=c(32,32)) + 
  geom_path(x=c(2,2), y=c(32,30)) + 
  annotate("text", x=1.5,y=34, label="**") + 
  ggtitle("Mean consumption for partition(1) and \n no partition(-1)") + 
  xlab("Partition") + 
  ylab("mean consumption") + 
  theme(plot.title=element_text(face="bold"))

plot of chunk unnamed-chunk-5

mean_svo <- ddply(Pdata, .(svo_mean, partition), summarize, mean=mean(consume))

ggplot(mean_svo, aes(y=mean, x=svo_mean, colour=factor(partition))) + 
  geom_line() + 
  ggtitle("Mean consumption for SVO") + 
  xlab("linear transformation of SVO ") + 
  ylab("Mean consumption for respective SVO") + 
  theme(plot.title=element_text(face="bold")) + 
  scale_colour_discrete(name="Partition", breaks=c("-1", "1"), labels=c("No partition", "Partition")) +
  geom_smooth(method=lm)

plot of chunk unnamed-chunk-5

ggplot(mean_svo, aes(y=mean, x=svo_mean, colour=factor(partition))) + 
  geom_point(shape=1) + 
  ggtitle("Mean consumption for SVO") + 
  xlab("linear transformation of SVO ") + 
  ylab("Mean consumption for respective SVO") + 
  theme(plot.title=element_text(face="bold")) + 
  scale_colour_discrete(name="Partition", breaks=c("-1", "1"), labels=c("No partition", "Partition")) +geom_smooth(method=lm)

plot of chunk unnamed-chunk-5

Analyzing the data

summary(glm(Pdata$coop ~ Pdata$partition*Pdata$svo_mean, family=binomial))
## 
## Call:
## glm(formula = Pdata$coop ~ Pdata$partition * Pdata$svo_mean, 
##     family = binomial)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -2.138  -0.973  -0.754   1.117   1.856  
## 
## Coefficients:
##                                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                     -0.2807     0.0966   -2.91  0.00367 ** 
## Pdata$partition                 -0.3409     0.0966   -3.53  0.00042 ***
## Pdata$svo_mean                  -3.1732     0.4599   -6.90  5.2e-12 ***
## Pdata$partition:Pdata$svo_mean   0.2195     0.4599    0.48  0.63310    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 691.16  on 504  degrees of freedom
## Residual deviance: 622.97  on 501  degrees of freedom
## AIC: 631
## 
## Number of Fisher Scoring iterations: 4
summary(glm(Pdata$consume ~ Pdata$partition*Pdata$svo_mean))
## 
## Call:
## glm(formula = Pdata$consume ~ Pdata$partition * Pdata$svo_mean)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -36.46  -12.71   -8.32   16.14   36.44  
## 
## Coefficients:
##                                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                      24.018      0.778   30.88  < 2e-16 ***
## Pdata$partition                  -2.380      0.778   -3.06   0.0023 ** 
## Pdata$svo_mean                  -27.820      3.441   -8.08  4.7e-15 ***
## Pdata$partition:Pdata$svo_mean    1.399      3.441    0.41   0.6845    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 304.9)
## 
##     Null deviance: 176199  on 504  degrees of freedom
## Residual deviance: 152739  on 501  degrees of freedom
## AIC: 4328
## 
## Number of Fisher Scoring iterations: 2
write.csv(Pdata, file="Default svo Data after Analysis.csv")