Advanced Modeling in R
Non-linear, Bayesian, and mixed effect methods
R. Condit, M. Ferrari
Cenpat, Patagonia
October 2012
Assignments
Tuesday AM, 9 October
Regression: Biomass data
Use log-transformed data
Possible terms
simple model: log(AGB) vs. log(dbh)
second order term: log(dbh) squared
rainfall and elevation
Graph
log(AGB) vs. log(dbh)
add curve of best fit
overlay curves for high rainfall and low rainfall
Character variable (factors)
use ForestType in the model
log
(
volume
)
~
log
(
dbh
) +
ForestType
compare to 3 independent models (3 forest types)
log
(
volume
[
dry
])
~
log
(
dbh
[
dry
])
etc.
Tuesday PM, 9 October
Fit a linear model with variable SD
Data
pupsize: Wt as a function of momcat
cecrin: growth (gr12) as a function of diameter (dbh1)
treemass: log(agb) as a function of log(dbh)
Likelihood function is provided (llike.linearmodel.full, in teaching.functions.r)
Requires 4 parameters: slope, intercept of model, then slope, intercept for SD
Fit a non-linear model to quantitative data
Model types
y
=
H
(it’s in teaching.functions.r)
y
~
x
+
log
x
(write yourself)
Data
pupsize: Wt is a non-linear function of momage
treeht: ht is a non-linear function of dbh (extract one species, eg quaras or tri2tu or pri2co)
Assume error is Gaussian (dnorm)
Save a graph of one or more of the model fits above and email to me (conditr@gmail.com)
Wednesday AM, 10 Octuber
Using llike.generalmodel, write and test a quadratic function,
a
+
bx
+
cx
2
, with any
of the data
Fit a two-parameter survival model
Data: cecrin
logit
(
status
3)
~
gr
12 +
dbh
2
(write yourself; see likelihood functions in teaching.functions.r for help)
Simulate regression with error in
x
Define a variable xerr as the standard deviation due to error
Use rnorm to generate xobs (contrast with xtrue)
Determine how estimated regression coefficient is affected
Simulate multi-level regression where slope varies between groups
Define a variable slopesd defining standard deviation of slope across groups
Use rnorm to simulate values of slope in three groups
Merge with rbind into one dataframe
Extra: write a loop to simulate a larger number of groups
Use lm for regression,
y
~
x
+
group
Use lmer for model,
y
~
1 +
x
+ (1 +
x
|
group
)
Extra credit: Simulate regression with two predictors,
x
1
and
x
2
Define a correlation between
x
1
and
x
2
Use rnorm to generate
x
2
then
y
Determine how estimated regression result is affected
Extra credit: Simulate Poisson regression
Define a variable xerr as the standard deviation due to error
Use rpois to generate xobs (contrast with xtrue)
Thursday, 11 October
Write a function to create 100 different populations with known size (N), known means, 2
levels of SD, normal distribution
Create a separate file to hold the function, to be sourced as updated, and with comments
Requires a loop of 100
Each step create one sample using the within-group SD
Start with one example where within-group means are identical
Then allow the group means to vary following an overarching normal distribution (the hyper-distribution)
Save results into one table
Table has 100 rows for 100 populations
Each row has 5 columns: N for population size, the known mean and SD, and the observed mean and SD
Repeat with different N’s (large or small sample) and varying the hyper-SD
Can you figure out how to repeat so the N’s vary?
Send me the file with function(s) via email
Friday, 12 October
Use lmer for regression of logagb on logdbh with species and forest type as
factors
Include squared term for logdbh
Add locality as a group effect (does it change the fixed effect)
Test forest type as fixed effect and as group effect
Graph points and lines
Compare alternative models
Use lmer for regression of pup Wt on momcat with year as a mixed effect
Variable intercept, slope, or both
Graph all points
Use xyplot for groups (Lattice)
Overlay lines of all random effects
Compare alternative models
Use lmer for regression of log(ht) on log(dbh) with species as a mixed effect
Saturday, 13 October
Fit a linear model to logagb vs log using the Bayesian method (modelfitBayes.r)
Use the function linearModelHier in growthModel.HierBayes to fit a hierarchical linear
model
Locality as the random (group) factor: use dataset treemass.split
Graph a line for each locality
The model will also work for growth data, using dataset BCIlist, but more slowly
Test the function survivalModel.Gibbs in survivalGibbs.r to fit a survival model (it’s
finished and should work)