Advertisement
Guest User

Untitled

a guest
Oct 5th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. runSim <- function(nTrials = 25, nDogs = 30,
  2. e0 = 100, s0 = 10,
  3. ks = 10, ke = 1){
  4. simDogs = matrix(nrow=nDogs, ncol=nTrials)
  5. for(i in 1:nDogs){
  6. e = e0; s = s0
  7. for(j in 1:nTrials){
  8. tot = e + s
  9. res = sample(0:1, 1, prob=c(s/tot, e/tot))
  10. if(res == 0){ s = s + ks}
  11. if(res == 1){ e = max(e - ke, 0)}
  12. simDogs[i, j] = res
  13. }
  14. }
  15. return(simDogs)
  16. }
  17.  
  18.  
  19. calcStats <- function(dat){
  20. stats = matrix(nrow = nrow(dat), ncol = 5)
  21. colnames(stats) = c("TrialsBeforeFirstAvoidance",
  22. "TrialsBeforeSecondAvoidance",
  23. "TotalShocks",
  24. "TrialofLastShock",
  25. "Alternations")
  26. for(i in 1:nrow(dat)){
  27. x = dat[i, ]
  28. stats[i, ] = c(which(x == 0)[1],
  29. which(x == 0)[2],
  30. sum(x),
  31. tail(which(x == 1), 1),
  32. sum(diff(x) != 0))
  33. }
  34. summary = t(apply(stats, 2, function(x) c(mean(x), sd(x))))
  35. colnames(summary) = c("Mean", "SD")
  36. return(list(stats = stats, summary = summary))
  37. }
  38.  
  39.  
  40. simDogs = runSim(nTrials = 25, nDogs = 30,
  41. e0 = 100, s0 = 10,
  42. ks = 25, ke = 15)
  43.  
  44. #realStats = calcStats(realDogs)
  45. simStats = calcStats(simDogs)
  46.  
  47. #print(realStats$summary, 3)
  48. print(simStats$summary, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement