From a7456f8a2d42f89044aec735798d62fe093ed6a7 Mon Sep 17 00:00:00 2001 From: AMoore202 Date: Sat, 4 Aug 2018 20:49:24 -0400 Subject: [PATCH 1/2] Added in automation and finished analysis Fixed up analysis overall and automated using custom functions. --- analysis/Analysis.R | 81 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/analysis/Analysis.R b/analysis/Analysis.R index 6a78f85..e27dbb4 100644 --- a/analysis/Analysis.R +++ b/analysis/Analysis.R @@ -4,16 +4,79 @@ FileThreat03Trail01 <- read.egt("text") DataThreat03Trail01 <- FileThreat03Trail01["results"] RepsThreat03Trail01 <- FileThreat03Trail01["reps"] -#Subset data to the final generation of the simulation -DataThreat03Trail01FinalGen <- subset(DataThreat03Trail01$results,gens == RepsThreat03Trail01) +library(plyr) +library(data.table) +library(ggplot2) -#Count the number of each contribution strategy into a vector. -CStratCountThreat03Trail01FinalGen <- c(nrow(subset(DataThreat03Trail01FinalGen,cstrats == 1)),nrow(subset(DataThreat03Trail01FinalGen,cstrats == 2)),nrow(subset(DataThreat03Trail01FinalGen,cstrats == 3))) +DatasetsListThreat05 <- list(DataThreat05Trial01,DataThreat05Trial02) +DatasetsListThreat25 <- list(DataThreat25Trial01,DataThreat25Trial02) +ListOfThreats <- list(DatasetsListThreat05,DatasetsListThreat25) +ThreatLevelList <- list(5,25) +LengthThreatLevelList <- length(ThreatLevelList) +ThreatLevelIndex <- c(1:LengthThreatLevelList) -#Count the number of each punishment strategy into a vector. -PStratCountThreat03Trail01FinalGen <- c(nrow(subset(DataThreat03Trail01FinalGen,pstrats == 1)),nrow(subset(DataThreat03Trail01FinalGen,pstrats == 2)),nrow(subset(DataThreat03Trail01FinalGen,pstrats == 3)),nrow(subset(DataThreat03Trail01FinalGen,pstrats == 4))) +CStratFunction <- function(ThreatList,Levels){ + CStratThreatLevelAnalysis <- function(TrialList){ + CStratProportionFunction <- function(Trial){ + NRow <- nrow(Trial) + Counts <- ddply(Trial,.(cstrats),.fun=nrow) + Counts[2] <- Counts[2] / NRow + Counts[,1][Counts[,1] == 1] <- "Contribute" + Counts[,1][Counts[,1] == 2] <- "Dissent" + Counts[,1][Counts[,1] == 3] <- "Opportunistic" + names(Counts) <- c("Contribution_Strategy","Proportion") + return(Counts) + } + AllTrialsList <- lapply(TrialList,CStratProportionFunction) + AllTrialsDF <- rbindlist(AllTrialsList) + CStratSummaryFunction <- function(DF){ + Summary <- c(Proportion=mean(DF[[2]]),sd=sd(DF[[2]])) + return(Summary) + } + SummaryTable <- ddply(AllTrialsDF,.(Contribution_Strategy),CStratSummaryFunction) + return(SummaryTable) + } + SummaryTableList <- lapply(ThreatList,CStratThreatLevelAnalysis) + AddThreatLevel <- function(i){ + SummaryTable <- data.frame(SummaryTableList[[i]],ThreatLevel = ThreatLevelList[[i]]) + return(SummaryTable) + } + SimultationDF <- rbindlist(lapply(Levels,AddThreatLevel)) + CStratPlot <- ggplot(data = SimulationDF, aes(x=Threat_Level, y=Proportion, group=Contribution_Strategy, colour=Contribution_Strategy)) + geom_line() + geom_point() + scale_x_continuous(breaks=seq(min(ThreatLevelList[[1]]),max(ThreatLevelList[[1]]), 20)) + geom_errorbar(aes(ymin=Proportion-(sd/2), ymax=Proportion+(sd/2)), width=1,position=position_dodge(0.05)) + return(CStratPlot) +} +PStratFunction <- function(ThreatList,Levels){ + PStratThreatLevelAnalysis <- function(TrialList){ + PStratProportionFunction <- function(Trial){ + NRow <- nrow(Trial) + Counts <- ddply(Trial,.(pstrats),.fun=nrow) + Counts[2] <- Counts[2] / NRow + Counts[,1][Counts[,1] == 1] <- "Responsibly" + Counts[,1][Counts[,1] == 2] <- "Anti_Socially" + Counts[,1][Counts[,1] == 3] <- "Spitefully" + Counts[,1][Counts[,1] == 4] <- "Never" + names(Counts) <- c("Punishment_Strategy","Proportion") + return(Counts) + } + AllTrialsList <- lapply(TrialList,PStratProportionFunction) + AllTrialsDF <- rbindlist(AllTrialsList) + PStratSummaryFunction <- function(DF){ + Summary <- c(Proportion=mean(DF[[2]]),sd=sd(DF[[2]])) + return(Summary) + } + SummaryTable <- ddply(AllTrialsDF,.(Punishment_Strategy),PStratSummaryFunction) + return(SummaryTable) + } + SummaryTableList <- lapply(ThreatList,PStratThreatLevelAnalysis) + AddThreatLevel <- function(i){ + SummaryTable <- data.frame(SummaryTableList[[i]],ThreatLevel = ThreatLevelList[[i]]) + return(SummaryTable) + } + SimultationDF <- rbindlist(lapply(Levels,AddThreatLevel)) + PStratPlot <- ggplot(data = SimulationDF, aes(x=Threat_Level, y=Proportion, group=Punishment_Strategy, colour=Punishment_Strategy)) + geom_line() + geom_point() + scale_x_continuous(breaks=seq(min(ThreatLevelList[[1]]),max(ThreatLevelList[[1]]), 20)) + geom_errorbar(aes(ymin=Proportion-(sd/2), ymax=Proportion+(sd/2)), width=1,position=position_dodge(0.05)) + return(PStratPlot) +} -CStratCountThreat03Trail01FinalGen -PStratCountThreat03Trail01FinalGen - +CStratFunction(ListOfThreats,ThreatLevelIndex) +PStratFunction(ListOfThreats,ThreatLevelIndex) From a6d0a11a62a24ff06215e90e2b28fceb4f7a5cb0 Mon Sep 17 00:00:00 2001 From: AMoore202 Date: Sun, 5 Aug 2018 21:08:24 -0400 Subject: [PATCH 2/2] Fixed analysis and cleaned up bugs Fixed analysis by integrating with the supplied datasets. Also added in three more temporary dummy datasets in order to perform the analysis. Minor bug fixes including typos. --- analysis/Analysis.R | 66 ++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/analysis/Analysis.R b/analysis/Analysis.R index e27dbb4..ae189d3 100644 --- a/analysis/Analysis.R +++ b/analysis/Analysis.R @@ -1,19 +1,49 @@ # Read data source(file.path("./read_egt.R"),chdir=F) -FileThreat03Trail01 <- read.egt("text") -DataThreat03Trail01 <- FileThreat03Trail01["results"] -RepsThreat03Trail01 <- FileThreat03Trail01["reps"] +FileThreat03Trial01 <- read.egt("text") +DataThreat03Trial01 <- FileThreat03Trial01["results"] +RepsThreat03Trial01 <- FileThreat03Trial01["reps"] + +# Created temporary dummy data to run analyses on +gens0302 <- c(1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5) +xs0302 <- c(5, 5, 1, 2, 4, 5, 5, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 1, 2, 4, 5, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5) +ys0302 <- c(4, 5, 2, 2, 5, 4, 5, 2, 1, 2, 4, 5, 4, 5, 4, 5, 1, 2, 3, 1, 2, 5, 1, 2, 3, 4, 5, 3, 4, 5, 3, 5, 3, 4, 5, 3, 4, 5, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 2, 3, 4, 5) +cstrats0302 <- as.factor(c(1, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 2, 1, 2, 3, 2, 1, 2, 2, 2, 1, 1, 2, 1, 3, 2, 3, 3, 2, 3, 3, 3, 3, 2, 2, 3, 2, 1, 1, 2, 3, 2, 3, 2, 1)) +pstrats0302 <- as.factor(c(3, 4, 3, 2, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 1, 2, 3, 4, 3, 2, 1, 3, 2, 3, 2, 1, 1, 3, 4, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1, 2, 1, 2, 2, 3, 4, 4, 4, 4, 2, 3, 2, 1, 2, 1, 2, 3)) +results0302 <- data.frame(gens0302, xs0302, ys0302, cstrats0302, pstrats0302) +names(results0302) <- c("gens","xs","ys","cstrats","pstrats") +FileThreat03Trial02 <- list(results0302, 5, 5, 0, 0.1, 0.1) +names(FileThreat03Trial02) <- c("results", "size", "reps", "egt.version", "mutation.rate", "death.rate") +gens2501 <- c(1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5) +xs2501 <- c(5, 5, 1, 2, 4, 5, 5, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5) +ys2501 <- c(4, 5, 2, 2, 5, 4, 5, 2, 1, 2, 4, 5, 4, 5, 4, 5, 1, 2, 3, 1, 2, 5, 1, 2, 3, 4, 5, 3, 4, 5, 3, 4, 5, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 2, 3, 4, 5) +cstrats2501 <- as.factor(c(2, 3, 3, 1, 3, 1, 1, 1, 2, 3, 2, 2, 2, 2, 3, 1, 2, 2, 3, 3, 2, 1, 2, 3, 3, 2, 2, 1, 1, 2, 1, 3, 2, 3, 3, 2, 3, 2, 2, 1, 3, 2, 1, 2, 2, 3, 2, 3, 1, 1, 1)) +pstrats2501 <- as.factor(c(4, 2, 2, 3, 4, 1, 1, 2, 3, 4, 2, 2, 3, 4, 1, 2, 1, 3, 2, 4, 3, 2, 2, 2, 2, 1, 4, 3, 3, 3, 1, 2, 2, 1, 1, 1, 3, 4, 3, 4, 3, 4, 3, 2, 1, 3, 1, 1, 1, 2, 2)) +results2501 <- data.frame(gens2501, xs2501, ys2501, cstrats2501, pstrats2501) +names(results2501) <- c("gens","xs","ys","cstrats","pstrats") +FileThreat25Trial01 <- list(results2501, 5, 5, 0, 0.1, 0.1) +names(FileThreat25Trial01) <- c("results", "size", "reps", "egt.version", "mutation.rate", "death.rate") +gens2502 <- c(1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5) +xs2502 <- c(5, 5, 1, 2, 4, 5, 5, 1, 2, 2, 3, 3, 4, 4, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5) +ys2502 <- c(4, 5, 2, 2, 5, 4, 5, 2, 1, 2, 4, 5, 4, 5, 4, 5, 1, 2, 3, 1, 2, 5, 1, 2, 3, 3, 4, 5, 3, 4, 5, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 2, 3, 4, 5) +cstrats2502 <- as.factor(c(2, 3, 3, 1, 3, 1, 1, 1, 2, 3, 2, 2, 2, 2, 3, 1, 2, 2, 3, 3, 2, 1, 2, 3, 3, 2, 2, 1, 1, 2, 1, 3, 2, 3, 3, 2, 3, 2, 2, 1, 3, 2, 1, 2, 2, 3, 2, 3, 1)) +cstrats2502 <- as.factor(c(2, 3, 2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1, 2, 3, 3, 2, 1, 2, 1, 2, 2, 3, 3, 2, 1, 2, 3, 3, 2, 2, 2, 1, 1, 2, 3, 2, 1, 3, 2, 1, 2, 2, 2, 2, 1, 1, 2, 3)) +pstrats2502 <- as.factor(c(2, 3, 4, 4, 4, 3, 2, 1, 2, 1, 3, 4, 1, 3, 1, 2, 3, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 4, 4, 3, 4, 3, 2, 1, 1, 3, 2, 4, 1, 3, 2, 1, 2, 3, 4, 3, 2, 2)) +results2502 <- data.frame(gens2502, xs2502, ys2502, cstrats2502, pstrats2502) +names(results2502) <- c("gens","xs","ys","cstrats","pstrats") +FileThreat25Trial02 <- list(results2502, 5, 5, 0, 0.1, 0.1) +names(FileThreat25Trial02) <- c("results", "size", "reps", "egt.version", "mutation.rate", "death.rate") library(plyr) library(data.table) library(ggplot2) -DatasetsListThreat05 <- list(DataThreat05Trial01,DataThreat05Trial02) -DatasetsListThreat25 <- list(DataThreat25Trial01,DataThreat25Trial02) -ListOfThreats <- list(DatasetsListThreat05,DatasetsListThreat25) -ThreatLevelList <- list(5,25) -LengthThreatLevelList <- length(ThreatLevelList) -ThreatLevelIndex <- c(1:LengthThreatLevelList) +DatasetsListThreat03 <- list(FileThreat03Trial01$results,FileThreat03Trial02$results) +DatasetsListThreat25 <- list(FileThreat25Trial01$results,FileThreat25Trial02$results) +ListOfThreats <- list(DatasetsListThreat03,DatasetsListThreat25) +ThreatLevelVector <- c(3,25) +LengthThreatLevelVector <- length(ThreatLevelVector) +ThreatLevelIndex <- c(1:LengthThreatLevelVector) CStratFunction <- function(ThreatList,Levels){ CStratThreatLevelAnalysis <- function(TrialList){ @@ -21,14 +51,14 @@ CStratFunction <- function(ThreatList,Levels){ NRow <- nrow(Trial) Counts <- ddply(Trial,.(cstrats),.fun=nrow) Counts[2] <- Counts[2] / NRow + levels(Counts[,1]) <- c("1","2","3","Contribute","Dissent","Opportunistic") Counts[,1][Counts[,1] == 1] <- "Contribute" Counts[,1][Counts[,1] == 2] <- "Dissent" Counts[,1][Counts[,1] == 3] <- "Opportunistic" names(Counts) <- c("Contribution_Strategy","Proportion") return(Counts) } - AllTrialsList <- lapply(TrialList,CStratProportionFunction) - AllTrialsDF <- rbindlist(AllTrialsList) + AllTrialsDF <- rbindlist(lapply(TrialList,CStratProportionFunction)) CStratSummaryFunction <- function(DF){ Summary <- c(Proportion=mean(DF[[2]]),sd=sd(DF[[2]])) return(Summary) @@ -38,10 +68,10 @@ CStratFunction <- function(ThreatList,Levels){ } SummaryTableList <- lapply(ThreatList,CStratThreatLevelAnalysis) AddThreatLevel <- function(i){ - SummaryTable <- data.frame(SummaryTableList[[i]],ThreatLevel = ThreatLevelList[[i]]) + SummaryTable <- data.frame(SummaryTableList[[i]],Threat_Level = ThreatLevelList[[i]]) return(SummaryTable) } - SimultationDF <- rbindlist(lapply(Levels,AddThreatLevel)) + SimulationDF <- rbindlist(lapply(Levels,AddThreatLevel)) CStratPlot <- ggplot(data = SimulationDF, aes(x=Threat_Level, y=Proportion, group=Contribution_Strategy, colour=Contribution_Strategy)) + geom_line() + geom_point() + scale_x_continuous(breaks=seq(min(ThreatLevelList[[1]]),max(ThreatLevelList[[1]]), 20)) + geom_errorbar(aes(ymin=Proportion-(sd/2), ymax=Proportion+(sd/2)), width=1,position=position_dodge(0.05)) return(CStratPlot) } @@ -52,6 +82,7 @@ PStratFunction <- function(ThreatList,Levels){ NRow <- nrow(Trial) Counts <- ddply(Trial,.(pstrats),.fun=nrow) Counts[2] <- Counts[2] / NRow + levels(Counts[,1]) <- c("1","2","3","4","Responsibly","Anti_Socially","Spitefully","Never") Counts[,1][Counts[,1] == 1] <- "Responsibly" Counts[,1][Counts[,1] == 2] <- "Anti_Socially" Counts[,1][Counts[,1] == 3] <- "Spitefully" @@ -59,8 +90,7 @@ PStratFunction <- function(ThreatList,Levels){ names(Counts) <- c("Punishment_Strategy","Proportion") return(Counts) } - AllTrialsList <- lapply(TrialList,PStratProportionFunction) - AllTrialsDF <- rbindlist(AllTrialsList) + AllTrialsDF <- rbindlist(lapply(TrialList,PStratProportionFunction)) PStratSummaryFunction <- function(DF){ Summary <- c(Proportion=mean(DF[[2]]),sd=sd(DF[[2]])) return(Summary) @@ -70,11 +100,11 @@ PStratFunction <- function(ThreatList,Levels){ } SummaryTableList <- lapply(ThreatList,PStratThreatLevelAnalysis) AddThreatLevel <- function(i){ - SummaryTable <- data.frame(SummaryTableList[[i]],ThreatLevel = ThreatLevelList[[i]]) + SummaryTable <- data.frame(SummaryTableList[[i]],Threat_Level = ThreatLevelVector[i]) return(SummaryTable) } - SimultationDF <- rbindlist(lapply(Levels,AddThreatLevel)) - PStratPlot <- ggplot(data = SimulationDF, aes(x=Threat_Level, y=Proportion, group=Punishment_Strategy, colour=Punishment_Strategy)) + geom_line() + geom_point() + scale_x_continuous(breaks=seq(min(ThreatLevelList[[1]]),max(ThreatLevelList[[1]]), 20)) + geom_errorbar(aes(ymin=Proportion-(sd/2), ymax=Proportion+(sd/2)), width=1,position=position_dodge(0.05)) + SimulationDF <- rbindlist(lapply(Levels,AddThreatLevel)) + PStratPlot <- ggplot(data = SimulationDF, aes(x=Threat_Level, y=Proportion, group=Punishment_Strategy, colour=Punishment_Strategy)) + geom_line() + geom_point() + scale_x_continuous(breaks=seq(min(ThreatLevelVector),max(ThreatLevelVector), 22)) + geom_errorbar(aes(ymin=Proportion-(sd/2), ymax=Proportion+(sd/2)), width=1,position=position_dodge(0.05)) return(PStratPlot) }