-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWeek 4 Script.R
More file actions
67 lines (59 loc) · 3.11 KB
/
Week 4 Script.R
File metadata and controls
67 lines (59 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#Let's create a list with sets of values drawn from normal distributions with increasing means
exampleList <- vector("list", length = 10) #make an empty list
for (i in 1:length(exampleList)) { #for loop using index
exampleList[[i]] <- rnorm(10, i, 0.5) #generate normal distrubution of data centered around index of list
}
#And lets calculate some summary statistics from each set of values:
means <- c() #empty vector to store means
std <- c()
for (vec in exampleList) { #for loop using values
means <- c(means, mean(vec)) #get the mean of each item in list
std <- c(std, sd(vec))
}
plot(means)
ggplot(data.frame(group = LETTERS[1:10], mean = means, sd = std)) + geom_point(aes(x = group, y = mean)) +geom_errorbar(aes(x = group, ymin = mean - sd, ymax = mean + sd)) #don't worry about this line of code, just check out the plot
#Now lets write some functions to move an agent. The recursive function and the function using "while" do the same thing!
move <- function(x, y, averageDist, sd) { #function to move randomly in x-yplane
dist <- rnorm(1, averageDist, sd)
angle <- runif(1, 0, 2*pi)
x <- x + dist*cos(angle)
y <- y + dist*sin(angle)
return(c(x,y))
}
moveDistanceRecursive <- function(location, averageDist, sd, endDist) {
#input: location is a dataframe w. x and y columns (numeric vectors). averageDist and sd are single numeric values.
## endDist is a single numeric value giving the distance from the start location the agent must move
#output: a dataframe w/ all locations reached
newLocation <- move(location[nrow(location),1], location[nrow(location),2], averageDist, sd)
distFromStart <- sqrt(sum((location[1,] - newLocation)^2)) #pythogorean theorum
if (distFromStart >= endDist) return(rbind(location, newLocation))
return(moveDistanceRecursive(rbind(location, newLocation),averageDist, sd, endDist))
}
moveDistanceWhile <- function(location, averageDist, sd, endDist) {
#input: location is a dataframe w. x and y columns (numeric vectors). averageDist and sd are single numeric values.
## endDist is a single numeric value giving the distance from the start location the agent must move
#output: a dataframe w/ all locations reached
while (distFromStart < endDist) {
newLocation <- move(location[nrow(location),1], location[nrow(location),2], averageDist, sd)
distFromStart <- sqrt(sum((location[1,] - newLocation)^2)) #pythogorean theorum
location = rbind(location, newLocation) #This line still runs on last loop!
}
return(location)
}
times <- 40
simulations <- vector("list", times)
lengths <- vector(length = times)
#Two methods for running movement simulations multiple times
for (i in 1:times) {
simulations[[i]] <- moveDistanceRecursive(data.frame(x=0, y =0), 1, 0.2, 3)
lengths[i] <- nrow(simulations[[i]])
}
plot(table(lengths))
##OR##
i = 1
while(i <= times) {
i = i+1
simulations[[i]] <- moveDistanceRecursive(data.frame(x=0, y =0), 1, 0.2, 3)
lengths[i] <- nrow(simulations[[i]])
}
plot(table(lengths))