-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,89 @@ | ||
library(glmmTMB) | ||
library(dplyr) | ||
library(purrr) | ||
library(broom.mixed) | ||
|
||
set.seed(101) | ||
## These ...'s aren't the best practice; should figure out more about map | ||
sim <- function(groupSize, days, β0, β_treat, β_day | ||
, sdint, sdslope, corr, sdres=2, ... | ||
){ | ||
|
||
nGroup <- 15 | ||
nBats <- nGroup*2 | ||
nGroup=3; days=seq(3, 60, by=3) | ||
β0=10; β_treat=10; β_day=2 | ||
sdint=3; sdslope=0.1; corr=0; sdres=2 | ||
nBats <- groupSize*2 ## Treatment and control | ||
|
||
batID <- as.factor(1:nBats) ## Make this look less like a number with paste() | ||
treatment <- as.factor(rep(c("control", "exercise"), each=nGroup)) | ||
batID <- as.factor(1:nBats) ## Make this look less like a number with paste() | ||
treatment <- as.factor(rep(c("control", "exercise"), each=groupSize)) | ||
|
||
tdat <- data.frame(batID, treatment) | ||
tdat <- data.frame(batID, treatment) | ||
|
||
dat <- expand.grid(batID=batID, day=days) | ||
dat <- expand.grid(batID=batID, day=days) | ||
|
||
dat <- full_join(dat, tdat, by="batID") | ||
dat <- full_join(dat, tdat, by="batID") | ||
|
||
y <- simulate_new( ~ 1 + treatment + day + (1 + day | batID) | ||
, nsim = 1 | ||
, family = "gaussian" | ||
, newdata = dat | ||
, newparams = list( | ||
beta = c(β0, β_treat, β_day) | ||
, theta = c(log(sdint), log(sdslope), corr) | ||
, betad = log(sdres) | ||
y <- simulate_new( ~ 1 + treatment + day + (1 + day | batID) | ||
, nsim = 1 | ||
, family = "gaussian" | ||
, newdata = dat | ||
, newparams = list( | ||
beta = c(β0, β_treat, β_day) | ||
, theta = c(log(sdint), log(sdslope), corr) | ||
, betad = log(sdres) | ||
) | ||
) | ||
) | ||
|
||
dat$flightTime <- y[[1]] | ||
dat$flightTime <- y[[1]] | ||
return(dat) | ||
} | ||
|
||
fit <- function(dat){ | ||
return(glmmTMB(flightTime ~ 1 + treatment + day + (1 + day | batID) | ||
, data=dat | ||
, family = "gaussian" | ||
)) | ||
} | ||
|
||
simCIs <- function(simfun, fitfun, ...){ | ||
dat <- simfun(...) | ||
fit <- fitfun(dat) | ||
c <- as.data.frame(confint(fit)) | ||
return(data.frame(vname=rownames(c) | ||
, est = c$Est | ||
, low = c$"2.5" | ||
, high = c$"97.5" | ||
)) | ||
} | ||
|
||
set.seed(101) | ||
nReps <- 1000 | ||
|
||
fit <- glmmTMB(flightTime ~ 1 + treatment + day + (1 + day | batID) | ||
, data=dat | ||
, family = "gaussian" | ||
print(simCIs(simfun=sim, fitfun=fit | ||
, groupSize = 15, days=seq(3, 60, by=3) | ||
, β0=10, β_treat=10, β_day=2 | ||
, sdint=3, sdslope=0.1, corr=0, sdres=2 | ||
)) | ||
|
||
cis <- map_dfr(1:nReps, simCIs, .id="sim", simfun=sim, fitfun=fit | ||
, groupSize = 15, days=seq(3, 60, by=3) | ||
, β0=10, β_treat=10, β_day=2 | ||
, sdint=3, sdslope=0.1, corr=0, sdres=2 | ||
) | ||
|
||
summary(fit) | ||
summary(cis) | ||
|
||
## saveRDS(cis, "sims.rds") | ||
## cis <- readRDS("sims.rds") | ||
|
||
## summary(cis |> mutate_if(is.character, as.factor)) | ||
|
||
confint(fit) | ||
## Check above | ||
treatmentTrue <- 10 | ||
|
||
## We are only interested in “power” to detect the true effect direction | ||
print(cis | ||
|> filter(vname=="treatmentexercise") | ||
|> summarize( | ||
toohigh=mean(low>treatmentTrue) | ||
, toolow=mean(high<treatmentTrue) | ||
, ci_width=mean(high-low) | ||
, power = mean(low>0) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters