Some students have been generous and shared their answers to homework – after the deadline, of course. Here you can see their answers and some of my comments.
These are some of the answers to Homework 6. We will share more homework answers later. If you want to share your homework’s answers, send me an email.
Most of the students prefer that answers are published anonimously. At the end, it is easier and better that all answers are shown without disclosing the author.
:::marginnote # Student 1 ::: |
# 1. The town is full of rats |
```r cat_and_rat <- function(N, birth_rate, catch_rate, death_rate, rats_ini, cats_ini) { cats <- d_cats <- rep(NA, N) rats <- d_rats <- rep(NA, N) cats[1] <- cats_ini rats[1] <- rats_ini d_cats[1] <- d_rats[1] <- 0 for(i in 2:N) { |
birth <- birth_raterats[i-1] catch <- catch_raterats[i-1]cats[i-1] death <- death_ratecats[i-1] |
d_rats[i] <- 2birth -birth - catch d_cats[i] <- 2catch -catch - death |
cats[i] <- cats[i-1] + d_cats[i] rats[i] <- rats[i-1] + d_rats[i] } return(data.frame(cats,rats)) } ``` |
## How does behavior change when conditions change? ```r initial_cats <- seq(from=10, to=500, by=10) |
behaviour <- function(cats_ini) { cats <- d_cats <- rep(NA, 3650) rats <- d_rats <- rep(NA, 3650) cats[1] <- cats_ini rats[1] <- 3000 d_cats[1] <- d_rats[1] <- 0 for(i in 2:3650) { |
birth <- 1.3/100rats[i-1] catch <- 1e-6rats[i-1]cats[i-1] death <- 0.7/100cats[i-1] |
d_rats[i] <- 2birth -birth - catch d_cats[i] <- 2catch -catch - death |
cats[i] <- cats[i-1] + d_cats[i] rats[i] <- rats[i-1] + d_rats[i] } return(data.frame(initial_cats,rats)) } changing <- lapply(initial_cats,behaviour) min_rats <- sapply(changing, min) rats <- data.frame(initial_cats,min_rats) plot(rats$min_rats, xlab=“Inıtıal Cats”, ylab=“Rats”, type=“l”) ``` |
## Bonus :::marginnote This is interesting. The Bonus does not have really any question. I’m curious why people answered a no-question. In those cases you should ask. ::: ```r min(town\(rats) max(town\)rats) sum(town$rats) |
plot(rats ~ cats, data=town) ``` |
# 2. When the cat is away, the rats will play ::: marginnote Here
the filename includes the full path. That is not recommended. It is
better to create a project for the homework and move the
sequence.txt file to the working directory. ::: |
r library(seqinr) genes <- read.fasta("C:/Users/Hp2020/Downloads/sequence.txt", seqtype= "DNA", set.attributes = FALSE) length(genes) |
## Calculate GC content |
## Find extreme values |
Use these indices to look inside names(genes) |
Student 2
Here the sections are not well marked. There must be
a space after the #
symbol.
#1. The town is full of rats
Here the function is defined with default values. This is not recommended, since it makes confusing to use the function.
<- function(N, birth_rate=1.3/100, catch_rate=1e-6,
cat_and_rat death_rate=0.7/100, rats_ini=3000, cats_ini=100){
<- d_rats <- rep(NA, N)
rats <- d_cats <- rep(NA, N)
cats
1] <- rats_ini
rats[1] <- cats_ini
cats[1] <- d_cats[1] <- 0
d_rats[
for(i in 2:N){
<- birth_rate*rats[i-1]
birth <- catch_rate*rats[i-1]*cats[i-1]
catch <- death_rate*cats[i-1]
death
<- catch - death
d_cats[i] <- birth - catch
d_rats[i]
<- rats[i-1] + d_rats[i]
rats[i] <- cats[i-1] + d_cats[i]
cats[i]
} return(data.frame(rats, cats))
}
How does behavior change when conditions change?
<- seq(from=10, to=10, by=10)
initial_cats
for(i in initial_cats){
<- rep(NA, length(initial_cats))
min_rats <- cat_and_rat(cats_ini = i, N=365)
min_rats min(town$rats)
return(min_rats)
}
plot(min_rats)
Bonus
plot(rats ~ cats, data=town)
2. When the cat is away, the rats will play
library(seqinr)
<- read.fasta("sequence.txt")
genes length(genes)
Calculate GC content
<- sapply(genes, GC) gc_genes
Find extreme values
which.max(gc_genes)
which.min(gc_genes)
This answer is wrong since it used fixed numbers. It should not. The code should work even if you change the FASTA file.
Use these indices to look inside names(genes)
names(gc_genes[970])
names(gc_genes[238])
Bonus
<- cumsum(gc_genes) acc_gc_genes
:::marginnote # Student 3 ::: |
---|
:::marginnote # Student 4 ::: |
# 1. The town is full of rats ```r cats_and_rats <- function(N, birth_rate, catch_rate, death_rate, cats_ini, rats_ini) { rats <- d_rats <- rep(NA, N) cats <- d_cats <- rep(NA, N) |
d_rats[1] <- d_cats[1] <- 0 |
cats[1] <- cats_ini rats[1] <- rats_ini |
for(i in 2:N){ d_rats[i] <- birth_raterats[i-1] - cats[i-1]rats[i-1]catch_rate d_cats[i] <- -death_ratecats[i-1] + cats[i-1]rats[i-1]catch_rate rats[i] <- rats[i-1] + d_rats[i] cats[i] <- cats[i-1] + d_cats[i] } return(data.frame(rats, cats)) } ``` |
## How does behavior change when conditions change? ```r initial_cats <- seq(from=10,to=500,by=10) min_rats <- rep(NA,length(initial_cats)) |
for(a in 1:length(initial_cats)){ town <- cats_and_rats(N=3650, rats_ini=3000, cats_ini=initial_cats[a], birth_rate=1.3/100, catch_rate=1e-6, death_rate=0.7/100) min_rats[a] <- min(town$rats) } plot(initial_cats,min_rats, type = “l”, ylab = “Rats”) ``` |
## Bonus ```r locate_half <- function(i){ mid <- (min(i) + max(i))/2 which(i >= mid) -> indices_over_half_value ans <- indices_over_half_value[1] return(ans) } |
min_rats <- rep(NA,length(initial_cats)) max_rats <- rep(NA,length(initial_cats)) sum_rats <- rep(NA,length(initial_cats)) |
min_rats[a] <- min(town\(rats) max_rats[a] <- max(town\)rats) sum_rats[a] <- sum(town$rats) |
plot(rats ~ cats, data=town) ``` |
# 2. When the cat is away, the rats will play |
## Calculate GC content ```r gc_genes <- function(i) { V <- toupper(genes[[i]]) count_C <- sum(V==“C”) count_G <- sum(V==“G”) GC_content <- (count_C +count_G)/length(V) return(GC_content) } |
gc_genes <- sapply(1:length(genes), gc_genes) ``` |
## Find extreme values |
## Use these indices to look inside
names(genes) |
Student 5
1. The town is full of rats
<- function(N, rats_ini, cats_ini, birth_rate,
cat_and_rat
catch_rate, death_rate) {<- rep(NA, N)
cats <- rep(NA, N)
rats 1] <- cats_ini
cats[1] <- rats_ini
rats[for(i in 2:N) {
<- cats[i-1] + cats[i-1]*catch_rate*rats[i-1] - cats[i-1]*death_rate
cats[i] <- rats[i-1] + rats[i-1]*birth_rate - rats[i-1]*catch_rate*cats[i-1]
rats[i]
}return(data.frame(cats, rats))
}
How does behavior change when conditions change?
What are the inputs to seq()
?
It is better to use the names of each parameter.
The paramters N
, rats_ini
,
birth_rate
, catch_rate
, and
death_rate
must be function inputs, not
fixed values.
<- seq(10, 500, 10)
initial_cats <- function(cats_ini) {
cat_and_rat2 =3650
N=3000
rats_ini=1.3/100
birth_rate=1e-6
catch_rate=0.7/100
death_rate<- rep(NA, N)
cats <- rep(NA, N)
rats 1] <- cats_ini
cats[1] <- rats_ini
rats[for(i in 2:N) {
<- cats[i-1] + cats[i-1]*catch_rate*rats[i-1] - cats[i-1]*death_rate
cats[i] <- rats[i-1] + rats[i-1]*birth_rate - rats[i-1]*catch_rate*cats[i-1]
rats[i]
}return(data.frame(rats))
}<- lapply(initial_cats, cat_and_rat2)
list_of_rat <- data.frame(initial_cats, sapply(list_of_rat, min))
min_rats plot(min_rats, ylab="Rats")
Bonus
<- data.frame(initial_cats, sapply(list_of_rat, max))
max_rats plot(max_rats, main="Max of Rats", ylab="Rats")
<- data.frame(initial_cats, sapply(list_of_rat, sum))
sum_rats plot(sum_rats, main="Sum of Rats", ylab="Rats")
2. When the cat is away, the rats will play
library(seqinr)
<- read.fasta("~/Downloads/sequence.txt")
genes length(genes)
Calculate GC content
<- rep(NA, length(genes))
gc_genes for(i in 1:length(genes)) {
<- GC(genes[[i]])
gc_genes[i] }
Find extreme values
which.max(gc_genes)
which.min(gc_genes)
Use these indices to look inside names(genes)
names(genes[which.max(gc_genes)])
names(genes[which.min(gc_genes)])
Bonus
<- rep(NA, length(gc_genes))
gc_genes2 for (i in 1:length(gc_genes)) {
<- gc_genes[i] - sum(gc_genes)/length(gc_genes)
gc_genes2[i]
}plot(gc_genes)
abline(h=mean(gc_genes))
abline(v=mean(which.max(cumsum(gc_genes2))))
abline(v=mean(which.min(cumsum(gc_genes2))))