diff --git a/07_final_assignment/baboonSimulation.R b/07_final_assignment/baboonSimulation.R index 05b480d..c9356df 100644 --- a/07_final_assignment/baboonSimulation.R +++ b/07_final_assignment/baboonSimulation.R @@ -6,7 +6,8 @@ NumNonwordsPresented=c(7832, 7832, 7832, 7832, 7832, 7832), GeneralAccuracy=c(79.81, 73.41, 72.43, 73.15, 71.55, 71.14), WordAccuracy=c(80.01, 74.83, 73.15, 79.26, 76.75, 75.38), - NonwordAccuracy=c(79.61, 72.00, 71.72, 67.06, 66.33, 66.90)) + NonwordAccuracy=c(79.61, 72.00, 71.72, 67.06, 66.33, 66.90), + stringsAsFactors=F) Resample <- function(x){ x[sample.int(length(x))] @@ -49,11 +50,23 @@ StimulusType=factor(character(BLOCK_SIZE), c("LearnedWord","NewWord","Nonword")), stringsAsFactors=F) + data <- data[,c("Stimulus","StimulusType")] stopifnot(is.character(data$Stimulus)) - result[1:25,] <- RandomPick(data[data$StimulusType == "NewWord",], 1) - result[26:50,] <- RandomPick(data[data$StimulusType == "LearnedWord",], 25) - result[51:100,] <- RandomPick(data[data$StimulusType == "Nonword",],50) + newSample <- RandomPick(data[data$StimulusType == "NewWord",], 1) + learnedSample <- RandomPick(data[data$StimulusType == "LearnedWord",], 25) + if( nrow(newSample) > 0 ) { + result[1:25,] <- newSample + } else { + result[1:25,] <- learnedSample + } + if( nrow(learnedSample) > 0 ){ + result[26:50,] <- learnedSample + } else { + result[26:50,] <- newSample + } + + result[51:100,] <- RandomPick(data[data$StimulusType == "Nonword",], 50) result <- result[sample(nrow(result)),] rownames(result) <- 1:nrow(result) @@ -92,12 +105,14 @@ isNotFirstBlock <- curBlock > 1 if ( isNotFirstBlock ){ newWordTrials <- block[block$StimulusType == "NewWord",] - stopifnot(length(unique(newWordTrials$Stimulus)) == 1) - wordResponses <- newWordTrials[newWordTrials$Response == "Word",] - wordResponseRate <- nrow(wordResponses) / nrow(newWordTrials) - if ( wordResponseRate >= 0.8 ){ - data$StimulusType[data$Stimulus == newWordTrials$Stimulus] <- "LearnedWord" - } + if( length(unique(newWordTrials$Stimulus)) == 1 ){ + newWordTrials <- newWordTrials[1,] + wordResponses <- newWordTrials[newWordTrials$Response == "Word",] + wordResponseRate <- nrow(wordResponses) / nrow(newWordTrials) + if ( wordResponseRate >= 0.8 ){ + data$StimulusType[data$Stimulus == newWordTrials$Stimulus] <- "LearnedWord" + } + } } block <- CreateBlock(data) block$Block <- curBlock @@ -216,5 +231,64 @@ print(monkey$GetWeights()) expect_equal(monkey$Rate("a"), "Word") expect_equal(monkey$Rate("c"), "Nonword") - }) +}) + +library(ndl) +dat <- read.table("dataDan.txt", header=T, stringsAsFactors=F) +dat$Cues <- orthoCoding(dat$String, grams=2) +dat$Frequency <- 1 +dat$Stimulus <- dat$Cues +dat$StimulusType <- factor(ifelse(dat$Type == "word", "NewWord", "Nonword"), c("Nonword","NewWord","LearnedWord")) +dat$Outcomes <- factor(ifelse(dat$Type == "word", "Word", "Nonword"), c("Word","Nonword")) + +dat <- dat[sample(1:nrow(dat)),] +cues <- unique(unlist(strsplit(dat$Cues, "_"))) +outcomes <- levels(dat$Outcomes) +monkey <- MakeMonkey(cues, outcomes, alpha=sqrt(0.001), beta=sqrt(0.001), lambda=1.0) +monkeyName <- "DAN" +trialNum <- round(0.5 * as.numeric(monkeydat[monkeydat$Monkey == monkeyName,"NumTrials"])) +system.time(pres <- PresentTrials( + trialNum + ,monkey$Learner,dat)) + +# Analyse data: +learnedWordNum <- nrow(pres$Stimuli[pres$Stimuli$StimulusType == "LearnedWord",]) +trialNum <- nrow(pres$Trials) +nonwordTrials <- pres$Trials[pres$Trials$StimulusType == "Nonword",] +wordTrials <- pres$Trials[pres$Trials$StimulusType != "Nonword",] +presNonwordNum <- length(unique(nonwordTrials$Stimulus)) +presWordNum <- length(unique(wordTrials$Stimulus)) +nonwordAcc <- nrow(nonwordTrials[nonwordTrials$Response == "Nonword",]) / nrow(nonwordTrials) +wordAcc <- nrow(nonwordTrials[wordTrials$Response == "Word",]) / nrow(wordTrials) +genAcc <- (nrow(nonwordTrials) * nonwordAcc + nrow(wordTrials) * wordAcc) / trialNum + +monkeydat <- rbind(monkeydat, + c(paste(monkeyName, "Sim1"), + trialNum, + learnedWordNum, + presNonwordNum, + genAcc, + wordAcc, + nonwordAcc)) + +# TODO: Das in schöne Funktionen packen ;) + +# TODO: Durchlaufen lassen für ca 50k Trials für verschiedene alpha, beta zwischen 0 und 1 +# TODO: Plotten der verschiedenen Ergebnisse (num,acc) als heatmap 2d plot. +# TODO: Plotten der Unterschiede zu den Vorgabeaffen und optimale alpha beta für diese finden. + +# what would be the theoretic success rates? +wRW <- monkey$GetWeights() +aRW <- estimateActivations(dat, wRW)$activationMatrix +aRW <- aRW[,order(colnames(aRW))] +dat$ChoiceRW <- apply(aRW, 1, FUN=function(v){ + if(v["Nonword"] >= v["Word"]) { + return("Nonword") + } else { + return("Word") + }}) +table(dat$Type, dat$ChoiceRW) + + # EOF +