Advanced Modeling in R

Non-linear, Bayesian, and mixed effect methods
R. Condit, M. Ferrari
Cenpat, Patagonia
October 2012

Assignments

Tuesday AM, 9 October

  1. Regression: Biomass data

Tuesday PM, 9 October

  1. Fit a linear model with variable SD
  2. Fit a non-linear model to quantitative data
  3. Save a graph of one or more of the model fits above and email to me (conditr@gmail.com)

Wednesday AM, 10 Octuber

  1. Using llike.generalmodel, write and test a quadratic function, a+bx+cx2, with any of the data
  2. Fit a two-parameter survival model
  3. Simulate regression with error in x
    1. Define a variable xerr as the standard deviation due to error
    2. Use rnorm to generate xobs (contrast with xtrue)
    3. Determine how estimated regression coefficient is affected
  4. Simulate multi-level regression where slope varies between groups
    1. Define a variable slopesd defining standard deviation of slope across groups
    2. Use rnorm to simulate values of slope in three groups
    3. Merge with rbind into one dataframe
    4. Extra: write a loop to simulate a larger number of groups
    5. Use lm for regression, y ~ x + group
    6. Use lmer for model, y ~ 1 + x + (1 + x|group)
  5. Extra credit: Simulate regression with two predictors, x1 and x2
    1. Define a correlation between x1 and x2
    2. Use rnorm to generate x2 then y
    3. Determine how estimated regression result is affected
  6. Extra credit: Simulate Poisson regression
    1. Define a variable xerr as the standard deviation due to error
    2. Use rpois to generate xobs (contrast with xtrue)

Thursday, 11 October

  1. Write a function to create 100 different populations with known size (N), known means, 2 levels of SD, normal distribution
    1. Create a separate file to hold the function, to be sourced as updated, and with comments
    2. Requires a loop of 100
      1. Each step create one sample using the within-group SD
      2. Start with one example where within-group means are identical
      3. Then allow the group means to vary following an overarching normal distribution (the hyper-distribution)
    3. Save results into one table
      1. Table has 100 rows for 100 populations
      2. Each row has 5 columns: N for population size, the known mean and SD, and the observed mean and SD
    4. Repeat with different N’s (large or small sample) and varying the hyper-SD
    5. Can you figure out how to repeat so the N’s vary?
    6. Send me the file with function(s) via email

Friday, 12 October

  1. Use lmer for regression of logagb on logdbh with species and forest type as factors
    1. Include squared term for logdbh
    2. Add locality as a group effect (does it change the fixed effect)
    3. Test forest type as fixed effect and as group effect
    4. Graph points and lines
    5. Compare alternative models
  2. Use lmer for regression of pup Wt on momcat with year as a mixed effect
    1. Variable intercept, slope, or both
    2. Graph all points
    3. Use xyplot for groups (Lattice)
    4. Overlay lines of all random effects
    5. Compare alternative models
  3. Use lmer for regression of log(ht) on log(dbh) with species as a mixed effect

Saturday, 13 October

  1. Fit a linear model to logagb vs log using the Bayesian method (modelfitBayes.r)
  2. Use the function linearModelHier in growthModel.HierBayes to fit a hierarchical linear model
    1. Locality as the random (group) factor: use dataset treemass.split
    2. Graph a line for each locality
    3. The model will also work for growth data, using dataset BCIlist, but more slowly
  3. Test the function survivalModel.Gibbs in survivalGibbs.r to fit a survival model (it’s finished and should work)