library(data.table)
## working directory should be: vu_advstats_students
tsuga = readRDS("data/tsuga.rds")
## if you don't have the repository saved locally
# tsuga = readRDS(url("https://github.com/ltalluto/vu_advstats_students/raw/main/data/tsuga.rds"))
This dataset gives statistics about Tsuga canadensis
observed over multiple years in forest plots in eastern Canada. Included
are the number of trees born
, the number observed to have
died
that year, the total number of trees (including dead
ones) n
, and the climate. Filter the dataset so that it
contains only observations from the year 2005 with at least 1 individual
(n > 0
)
p
: a single value, the probability that a
randomly chosen individual is deadn
: a vector, the number of trees in each
plotk
: a vector, the number of dead trees in each
plot# n and k are vectors
lfun = function(p, n, k) {
## function body
return() ## a single value!
}
p
lfun
once
for each value of p
. This is most efficiently accomplished
using sapply
, but a for
loop will also
work.optim
to find the MLE for p
mean(dat$died/dat$n)
? If
so, why?optim
.optimizing
.mean(dat$n)
?r****()
function
corresponding to the likelihood function to simulate a new dataset, with
as many observations as in the original data.