The statistical model:
\(y_t = \beta_0 + \beta_1 * (Elevation_s)_t + \beta_2 * Slope_t + (b_s)_t + \epsilon_t\)
Where:
Let’s define the parameters:
nstand = 5
nplot = 4
b0 = -1
b1 = .005
b2 = .1
sds = 2
sd = 1
Simulate other variables:
set.seed(16)
stand = rep(LETTERS[1:nstand], each = nplot)
standeff = rep( rnorm(nstand, 0, sds), each = nplot)
ploteff = rnorm(nstand*nplot, 0, sd)
Simulate elevation and slope:
elevation = rep( runif(nstand, 1000, 1500), each = nplot)
slope = runif(nstand*nplot, 2, 75)
Simulate response variable:
resp2 = b0 + b1*elevation + b2*slope + standeff + ploteff
Your tasks (complete each task in its’ own code chunk, make sure to use echo=TRUE so I can see your code):
# Loading the libraries required
library(lmerTest)
## Warning: package 'lmerTest' was built under R version 3.5.3
## Loading required package: lme4
## Warning: package 'lme4' was built under R version 3.5.3
## Loading required package: Matrix
##
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
##
## lmer
## The following object is masked from 'package:stats':
##
## step
library(lme4)
# Creating a model and running it to view observations
mod1 <- lmer(resp2 ~ 1 + elevation + slope + (1|stand))
summary(mod1)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: resp2 ~ 1 + elevation + slope + (1 | stand)
##
## REML criterion at convergence: 82
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.65583 -0.62467 -0.01693 0.53669 1.41736
##
## Random effects:
## Groups Name Variance Std.Dev.
## stand (Intercept) 1.208 1.099
## Residual 1.358 1.165
## Number of obs: 20, groups: stand, 5
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) -21.314628 6.602053 3.001313 -3.228 0.0482 *
## elevation 0.020600 0.004916 3.113482 4.190 0.0230 *
## slope 0.095105 0.016441 15.868032 5.785 2.88e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) elevtn
## elevation -0.991
## slope 0.049 -0.148
# Checking for parameters to crosscheck comparison.
cat("b0 = ", b0, sep = "")
## b0 = -1
# Running another sample
cat("b1 = ", b1, sep = "")
## b1 = 0.005
# Checking b^2 next
cat("b2 = ", b2, sep = "")
## b2 = 0.1
Notice observation clearly shows b^2 being 0.1 near to the original parameters compared to b^1 thats shows 0.005.
mod_sim = function(nstand = 5, nplot = 4, b0 = -1, b1 = 0.005, b2 = 0.1, sds = 2, sd = 1) {
stand = rep(LETTERS[1:nstand], each = nplot)
sim_standeff = rep(rnorm(nstand, 0, sds), each = nplot)
sim_ploteff = rnorm(nstand * nplot, 0, sd)
elevation = rep(runif(nstand, 1000, 1500), each = nplot)
sim_slope = runif(nstand * nplot, 2, 75)
sim_resp2 = b0 + b1 * elevation + b2 * sim_slope + sim_standeff + sim_ploteff
simdata = data.frame(sim_resp2, elevation, sim_slope, stand)
lmer(sim_resp2 ~ 1 + elevation + sim_slope + (1|stand), data = simdata)
}
# Runnign the model to check simulations.
mod_sim()
## Linear mixed model fit by REML ['lmerModLmerTest']
## Formula: sim_resp2 ~ 1 + elevation + sim_slope + (1 | stand)
## Data: simdata
## REML criterion at convergence: 80.9781
## Random effects:
## Groups Name Std.Dev.
## stand (Intercept) 2.3573
## Residual 0.9754
## Number of obs: 20, groups: stand, 5
## Fixed Effects:
## (Intercept) elevation sim_slope
## 10.584601 -0.005464 0.086839
# Next lets run 1000 simulations
result_1000 = replicate(n = 1000, expr = mod_sim())
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00350336
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.0045514
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00363544
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00439561
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00239513
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00219495
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00322495
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
# Lets check the length next
length(result_1000)
## [1] 1000
# Loading more libraries for further analysis
library(broom.mixed)
## Warning: package 'broom.mixed' was built under R version 3.5.3
## Warning in checkMatrixPackageVersion(): Package version inconsistency detected.
## TMB was built with Matrix version 1.2.17
## Current Matrix version is 1.2.14
## Please re-install 'TMB' from source using install.packages('TMB', type = 'source') or ask CRAN for a binary version of 'TMB' matching CRAN's 'Matrix' package
library(broom)
## Warning: package 'broom' was built under R version 3.5.3
##
## Attaching package: 'broom'
## The following object is masked from 'package:broom.mixed':
##
## tidyMCMC
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages ----------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.0 v purrr 0.3.2
## v tibble 2.1.3 v dplyr 0.8.3
## v tidyr 0.8.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.5.3
## Warning: package 'tibble' was built under R version 3.5.3
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'readr' was built under R version 3.5.3
## Warning: package 'purrr' was built under R version 3.5.3
## Warning: package 'dplyr' was built under R version 3.5.3
## Warning: package 'stringr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts -------------------------------------------------------------------- tidyverse_conflicts() --
## x tidyr::expand() masks Matrix::expand()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x broom::tidyMCMC() masks broom.mixed::tidyMCMC()
# Now, lets print the first 6 rows of the data (n=6)
var = result_1000 %>% map_dfr(tidy, effects = "ran_pars", scales = "vcov")
var %>% print(n = 6)
## # A tibble: 2,000 x 4
## effect group term estimate
## <chr> <chr> <chr> <dbl>
## 1 ran_pars stand var__(Intercept) 2.61
## 2 ran_pars Residual var__Observation 1.11
## 3 ran_pars stand var__(Intercept) 9.73
## 4 ran_pars Residual var__Observation 1.36
## 5 ran_pars stand var__(Intercept) 0.827
## 6 ran_pars Residual var__Observation 0.914
## # ... with 1,994 more rows
# Installing libraries
library(furrr)
## Warning: package 'furrr' was built under R version 3.5.3
## Loading required package: future
## Warning: package 'future' was built under R version 3.5.3
library(ggplot2)
plan(multiprocess)
vizlist = c(5, 30, 100) %>%
set_names(c("size_5", "size_30", "size_100")) %>%
future_map(function(.size) replicate(n = 1000, expr = mod_sim(nstand = .size)))
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00295659
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.0027045
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00215284
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00395311
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00417219
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00245866
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00210622
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00395452
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00305796
## (tol = 0.002, component 1)
## boundary (singular) fit: see ?isSingular
## boundary (singular) fit: see ?isSingular
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00207284
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.0146987
## (tol = 0.002, component 1)
result_df = vizlist %>%
modify_depth(.depth = 2, function(x) tidy(x, effects = "ran_pars", scales = "vcov")) %>%
map_dfr(bind_rows, .id = "id") %>%
filter(group == "stand")
result_df
## # A tibble: 3,000 x 5
## id effect group term estimate
## <chr> <chr> <chr> <chr> <dbl>
## 1 size_5 ran_pars stand var__(Intercept) 4.92
## 2 size_5 ran_pars stand var__(Intercept) 0.721
## 3 size_5 ran_pars stand var__(Intercept) 0.582
## 4 size_5 ran_pars stand var__(Intercept) 5.68
## 5 size_5 ran_pars stand var__(Intercept) 1.85
## 6 size_5 ran_pars stand var__(Intercept) 6.27
## 7 size_5 ran_pars stand var__(Intercept) 3.56
## 8 size_5 ran_pars stand var__(Intercept) 7.24
## 9 size_5 ran_pars stand var__(Intercept) 0.0443
## 10 size_5 ran_pars stand var__(Intercept) 0.888
## # ... with 2,990 more rows
result_df %>%
mutate(
id = case_when(
id == "size_5" ~ "size = 5",
id == "size_30" ~ "size = 30",
id == "size_100" ~ "size = 100"
)
) %>%
ggplot(aes(x = estimate) ) +
geom_density(fill = "blue", alpha = .25) +
facet_wrap(~ id) +
geom_vline(xintercept = 4) +
theme_bw()
Based on the graphs with the increase in the sample size estimates shows initial value close to 4.