-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlesson_2.R
156 lines (130 loc) · 4.21 KB
/
lesson_2.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
#1 loop that prints numbers 20 to 10
for (i in 20:10){
print(i)
}
#2 calcluate if a number is even
for (i in 20:10){
if (i %% 2 == 0){
print(i)
}
}
#3 calculate if a number is a prime number
prime <- function(x){
for (i in (x-1):2){
n <- x %% i
if(n == 0 | x == 1){
return(FALSE)
}
}
return(TRUE)
}
#4 loop that determines if a number is divisible by five or a prime number
for (i in 1:20){
if (i %% 5 == 0){
cat("Good:", i, "\n")
}
if(prime(i) == TRUE){
cat("Job:", i, "\n")
}
}
#5 function that calculates pop size using the Gompertz curve
gompPop <- function(time, a, b, c){
return(a * exp(-b * exp(-c * time)))
}
#6 plot the progression of #5 over time
#use the same inputs as gompPop
#loop through the f(x) for each value of time
#Thank You Marley Haupt,
#also for loops are sometimes unecessary!!!
gompTime <- function(time, a, b, c){
t <- c(1:time)
plot(t, gompPop(t,a,b,c), xlab="Time", ylab="Pop Size", main="Gomp Style", type = "l")
}
#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.
gompMagic <- function(time, a, b, c){
t <- c(1:time)
color <- "black"
color[gompPop(t,a,b,c) > a] <- "blue"
color[gompPop(t,a,b,c) > b] <- "red"
plot(t, gompPop(t,a,b,c), xlab="Time", ylab="Pop Size", main="Gomp Style", type = "l", col = color)
}
#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.
gompPurp <- function(time, a, b, c){
t <- c(1:time)
color <- "black"
color[gompPop(t,a,b,c) > a & gompPop(t,a,b,c) > b] <- "purple"
plot(t, gompPop(t,a,b,c), xlab="Time", ylab="Pop Size", main="Gomp Style", type = "l", col = color)
}
#9 f(x) that draws boxes with a specified h & w
box <- function(height, width){
cat(rep("*",width),"\n")
for (i in 1:(height-2)){
cat("*",rep(" ",(width-2)),"*","\n")
}
cat(replicate(width,"*"),"\n")
}
#10. Modify your box function to put text centred inside the box
# count the number of characters in the word input, define as width
#replicate = evaluate this expresstion x times
#use rep here
#if nchar() > width, use nchar
#h & w made even so the text can
wordBox <- function(height, width, word){
#make the height odd to properly center the word
if (height %% 2 == 0){
height <- height + 1
}
n <- nchar(word)
x <- width/2
if (n > (width-2)){
print("need a wider box")
}else{
cat(rep("*",width),"\n")
for (i in 1:(floor(height/2)-1)){
cat("*",rep(" ",(width-2)),"*")
cat("\n")
}
cat("*",rep(" ",x),word,rep(" ",x),"*")
for (i in (floor(height/2)-2:height)){
cat("*",rep(" ",(width-2)),"*")
cat("\n")
}
cat(rep("*",width),"\n")
}
}
}
if (is.integer(x)){
cat("*",rep(" ",x),word,rep(" ",x),"*")
}else{
cat("*",rep(" ",(x+1)),word,rep(" ",(x+1)),"*")
}
}
#11
#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 at a site (drawn, for example,
#from a Bernoulli distribution) and then model the abundance for any species that is 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 λ. Hint: there is no Bernoulli distribution in R, but the Bernoulli is a
#special case of what distribution?...
#model p of species at a site (binom) -> 1 = species presence
#if present, model abundance for any species that is present
abundance <- function(sites){
y <- c(1:sites)
for (i in 1:sites){
p[i] <- rbinom(1,1,0.5)
if (p = 1){
a <- rpois(1,runif(1,min=0))
}else{
return(p)
}
}
}
#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 λ ) 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).