-
Notifications
You must be signed in to change notification settings - Fork 0
/
r-exercise2-marleyhaupt1.R
402 lines (380 loc) · 13.9 KB
/
r-exercise2-marleyhaupt1.R
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
## 1. Write a loop that prints out the numbers from 20 to 10
for (i in 20:10){
print(i)
}
## 2. Write a loop that prints out only the numbers 20 to 10 that are even
for (i in 20:10){
if (i %% 2 == 0){
print(i)
}
}
## 3. Write a function that calculates whether a number is a prime number
prime<-function(x){
for (i in (x-1):2){
r<-x %% i
if(r == 0){
return(FALSE) # If there is ever a mod with a remainder of 0 then the number is not prime and function will return FALSE and stop
}
}
return(TRUE) # If the mod never turns up as 0 then the number is prime and the function will return TRUE
}
## 4. Write a loop that prints out the numbers from 1 to 20, printing
# "Good: NUMBER" if the number is divisible by five and "Job: NUMBER"
# if the number is prime, and nothing otherwise.
for (i in 1:20){
f<-i %% 5 # Checking if all of the numbers are divisable by 5
if(f == 0){
print("Good: NUMBER")
print(i)
}
t<-TRUE # Creating a number to test for prime (true=prime)
for(j in 2:(i-1)){
p<-i %% j
if(p == 0){ # This is basically the same as what I did in question 3
t<-FALSE
break
}
}
if(t == TRUE || i == 1){
print("Job: NUMBER")
print(i)
}
}
## 5. A biologist is modeling population growth using a Gompertz curve, which
# is defined as y(t) = a.e^−b.e^−c.t where y is population size, t is time,
# a and b are parameters, and e is the exponential function. Write them a
# function that calculates population size at any time for any values of its
# parameters.
gompertz<-function(time, a, b, c){
y<-a*exp(-b*exp(-c*time))
return(y)
}
## 6. The biologist likes your function so they want you to write another
# function that plots the progress of the population over a given length of
# time. Write it for them.
plotgomp<-function(start,end,by,a,b,c){
time<-seq(start,end,by) # Creates a vector with sequenctial values of time between a specified start and end by any increment
popsize<-c(NA) # Creates a vector with the same length as the vector of time
for(i in 1:length(time)){
y<-gompertz(start+(i-1)*by,a,b,c)
popsize[i]<-y # Populates the vector "popsize" with the values calcuated in the gompertz equation
}
plot(x=time, y=popsize, xlab="Time",
ylab="Population Size", main="Gompertz Plot")
abline(h=a, lty=2, col="blue") # Adds a blue horizontal line that intersects the y axis at the value of the parameter a
abline(h=b, lty=2, col="red") # Adds a red horizontal line that intersects the y axis at the value of the parameter b
}
## 7. The biologist has fallen in love with your plotting function, but wants
# to color y values above a as blue and y values above b as red. Change your
# function to allow that.
plotgomp<-function(start,end,by,a,b,c){
time<-seq(start,end,by)
popsize<-c(NA)
color<-c(NA)
for(i in 1:length(time)){
y<-gompertz(start+(i-1)*by,a,b,c)
popsize[i]<-y
if(y>=a){
color[i]<-"blue"
} else {
color[i]<-"black"
}
if (y>=b){
color[i]<-"red"
} else {
color[i]<-"black"
}
}
plot(x=time, y=popsize, xlab="Time", ylab= "Population Size", main= "Gompertz Plot", col=color)
abline(h=a, col="blue", lty=2)
abline(h=b, col="red", lty=2)
}
## 8. You are beginning to suspect the biologist is taking advantage of you.
# Modify your function to plot in purple any y value that is above a and b.
# Hint: Try putting 3==3 & 2==2 and 3==4 | 2==2 into an if statement and see
# what you get. Using this construction may make this simpler.
plotgomp<-function(start,end,by,a,b,c){
time<-seq(start,end,by)
popsize<-c(NA)
color<-c(NA)
for(i in 1:length(time)){
y<-gompertz(start+(i-1)*by,a,b,c)
popsize[i]<-y
if(y>=a){
color[i]<-"blue"
} else {
color[i]<-"black"
}
if (y>=b){
color[i]<-"red"
} else {
color[i]<-"black"
}
if (y>= a && b){
color[i]<-"purple"
}
}
plot(x=time, y=popsize, xlab="Time", ylab= "Population Size", main= "Gompertz Plot", col=color)
abline(h=a, col="blue", lty=2)
abline(h=b, col="red", lty=2)
}
## 9. Write a function that draws boxes of a specified height and look like this
# (height 3, width 5):
# *****
# * *
# *****
box<-function(height,width){
for (h in 1:height){
w<-1 # Create Index for my While Loop
while(w<=width){
if(h==1 || h==height || w==1 || w==width){ # Only put * on the border ( || means or)
cat("*")
} else{ # If not on the border put a blank space inside the box ("" is not the same as " ")
cat(" ")
}
w<-w+1 # Prevents an infinite loop, and a sad sad day for R
}
cat("\n") # For every new iteration of my for loop, create a new line
}
}
## 10. Modify your box function to put text centered inside the box, like this:
# *****************
# * *
# * some text *
# * *
# *****************
box.text<-function(height,width,words){
width<-max(width,nchar(words)+2) # Makes the box fit the words incase the words don't fit the box (nchar counts the number of characters in the text)
height<-max(height,3) # Makes the height of the box equal to the larger of either the height or 3 (3 so that the word will fit in the box)
for (h in 1:height){
w<-1
while(w<=width){
if(h==1 || h==height || w==1 || w==width){
cat("*")
} else{ # Add words to the center of the box. Ceiling rounds up, floor rounds down
if(h==ceiling(height/2) # Puts the words in the middle of the box vertically
&& w==ceiling(width/2)-ceiling(nchar(words)/2)){ # Puts the words in the middle of the box horizontally and makes it center instead of left align
cat(words) # Prints the words from the function
w<-w+nchar(words)-1 # Gets rid of blank spaces so the box is actually a box and not something funky
} else {
cat(" ")
}
}
w<-w+1
}
cat("\n")
}
}
## 11. Modify your box function to build boxes of arbitrary text, taking the
# demensions specified in terms of demensions, not the text. For example,
# box ("wdp", 3, 9, "hey") might produce:
# wdpwdpwdp
# w hey w
# wdpwdpwdp
box.text.b<-function(border,height,width,words){
width<-max(width,nchar(words)+2)
height<-max(height,3)
for (h in 1:height){
w<-1
while(w<=width){
if(h==1 || h==height || w==1 || w==width){
if(h==1 || h==height){ # If the top or bottom of the box then use the text from the border parameter in the function
b<-((w-1)%%nchar(border))+1 # Makes b=1 when w=0 and then loops through the rest of the letters in the border parameter
} else{
b<-1 # If in the center rows, always start with the first letter of the border parameter
}
cat(substr(border,b,b)) # substr is a function that recognizes each letter in a word, cat then prints that letter based on the index "b" which we defined previously
} else {
if(h==ceiling(height/2)
&& w==ceiling(width/2)-floor(nchar(words)/2)){
cat(words)
w<-w+nchar(words)-1
} else {
cat(" ")
}
}
w<-w+1
}
cat("\n")
}
}
## 12. In ecology, hurdle models are often used to model the abundance of species
# found on surveys. They first model the probability that a species will be
# present (drawn, for example, from the Poisson distribution). Write a function
# that simulates the abundance of a species at n sites given a probability of
# presence (p) and that its abundance is drawn from a Poisson with a given
# (lambda). Hint: there is no bernoulli distribution in R, but the Bernoulli is
# a special case of what distribution?...
hurdle.func<-function(n, lambda){
a <- list()
p <- runif(1)
for (i in 1:n){
if( p >= 0.4){
abund <- rpois(n=1, lambda)
} else {
abund <- 0
}
a <- append(a, abund)
}
return(a)
}
## 13. An ecologist really likes your hurdle function (will you never learn?).
# Write them a function that simulates lots of species (each with their own p
# and lambda) across n sites. Return the results in a matrix where each species
# is a column, and each site a row (this is the standard used for ecology data
# in R).
ss.hurdle <- function (species, sites, lambda){
a <- matrix(1, sites, species)
for (i in 1:species){
p <- runif(1)
for (j in 1:sites){
if( p >= 0.4){
abund <- rpois(n=1, lambda)
} else {
abund <- 0
}
a[j,i] <- abund
}
}
return(a)
}
## 14. Professor Savitzky approaches you with a delicate problem. A member of faculty
# became disoriented during fieldwork, and is now believed to be randomly
# wandering somewhere in the desert surrounding Logan. He is modelling their
# progress through time in five minute intervals, assuming they cover a random,
# Normally-distributed distance in latitude and longitude in each interval. Could
# you simulate this process 100 times and plot it for him?
lost_prof<-function(speed.mph){
xlist<-list() #Creates an empty list for the x values
ylist<-list() #Creates an empty list for the y values
start.x<-0
start.y<-0
s<-speed.mph*5280/12 #Takes a speed and converts it to the # of ft traveled in 5min
for (i in 1:100){
dist<-rnorm(1,s) #Randomly simulates the number of ft traveled every 5min
dir<-round(runif(1,1,4)) #Randomly determines a direction traveled
if(dir==1){
start.x<-start.x+dist
}
if(dir==2){
start.x<-start.x-dist
}
if(dir==3){
start.y<-start.y+dist
}
if(dir==4){
start.y<-start.x-dist
}
xlist<-append(xlist, start.x)
ylist<-append(ylist, start.y)
}
plot(x=xlist, y=ylist, col="red", type="l", main="Don't Die Professor")
}
## 15. Professor Savitzky is deeply concerned to realise that the member of faculty
# was, in fact, at the top of a steep mountain in the fog. Approximately 5 miles
# away, in all directions, from the faculty member's starting point is a deadly
# cliff! He asks if you could run your simulation to see how long, on average,
# until the faculty member plummets to their doom.
dead_prof<-function(speed.mph){
start.x<-0
start.y<-0
ft<-speed.mph*5280/12
time<-0
while (start.x <= 26400 || start.x >= -26400 ||
start.y <= 26400 || start.y >= -26400){
dist<-rnorm(1,ft)
dir<-round(runif(1,1,4))
if(dir==1){
start.x<-start.x+dist
if(start.x >= 26400){
return(time*5)
}
}
if(dir==2){
start.x<-start.x-dist
if(start.x <= -26400){
return(time*5)
}
}
if(dir==3){
start.y<-start.y+dist
if(start.y >= 26400){
return(time*5)
}
}
if(dir==4){
start.y<-start.x-dist
if(start.y <= -26400){
return(time*5)
}
}
time<-time+1
}
}
## 16. Sadly, by the time you have completed your simulation the faculty member
# has perished. Professor Savitzky is keen to ensure this will never happen again,
# and so has suggested each faculty member be attached, via rubber band, to a
# pole at the center of the site whenever conducting fieldwork. He assures you
# that you can model this by assuming that the faculty member, at each time-step
# moves alpha * distance-from-pole latitudinally and longitudinally (in
# additon to the rate of movement you've already simulated) each time-step.
# Simulate this, and see how strong the rubber band (alpha) must be to keep
# the faculty member safe for at least a day.
rubber.prof<-function(speed.mph){
start.x <- 0
start.y <- 0
ft<-speed.mph*5280/12
alpha <- 1
time <- 0
while (time <= 1440){
while (start.x <= 26400 || start.x >= -26400 ||
start.y <= 26400 || start.y >= -26400){
dist<-rnorm(1,ft)
dir<-round(runif(1,1,4))
if(dir==1){
start.x<-start.x+dist
hypot.sq <- start.x^2 + start.y^2
hypot <- sqrt(hypot.sq)
alpha <- alpha * hypot
if(start.x >= 26400){
return(c(alpha, time))
}
}
if(dir==2){
start.x<-start.x+dist
hypot.sq <- start.x^2 + start.y^2
hypot <- sqrt(hypot.sq)
alpha <- alpha * hypot
if(start.x >= 26400){
return(c(alpha, time))
}
}
if(dir==3){
start.x<-start.x+dist
hypot.sq <- start.x^2 + start.y^2
hypot <- sqrt(hypot.sq)
alpha <- alpha * hypot
if(start.x >= 26400){
return(c(alpha,time))
}
}
if(dir==4){
start.x<-start.x+dist
hypot.sq <- start.x^2 + start.y^2
hypot <- sqrt(hypot.sq)
alpha <- alpha * hypot
if(start.x >= 26400){
return(c(alpha,time))
}
}
time<-time+5
}
}
}
## 17. (If you finish early: Most, if not all, faculty members are not as young
# as they once were. See what effect the faculty member tiring (and
# eventually sitting down and giving up) would have on their likelihood of
# survival. What would happen if a faculty member panicked and walked faster
# through time?)
## Bonus Exercises: Your loops will go a lot faster in R if you pre-allocate your output
# variables....