Created
January 5, 2017 21:42
-
-
Save padpadpadpad/d5c8ec5352b77028938fe3c320d8e87b to your computer and use it in GitHub Desktop.
Calculate confidence intervals around parameters of an nlsLoop model fit
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
# function to get confidence intervals | |
confint_nlsLoop <- function(x, params, data, id_col, formula, ...){ | |
dat <- data[data[,id_col] == x,] | |
params2 <- params[params[,id_col] == x,] | |
fit = NULL | |
try(fit <- minpack.lm::nlsLM(formula, | |
data = dat, | |
start = params2[colnames(params2) %in% all.vars(formula[[3]])]), silent = TRUE) | |
if(!is.null(fit)){ | |
confint <- nlstools::confint2(fit) | |
confint <- data.frame(confint) | |
confint$param <- row.names(confint) | |
confint[,id_col] <- x | |
colnames(confint)[1:2] <- c('conf.lwr', 'conf.upr') | |
rownames(confint) <- NULL | |
return(confint) | |
} | |
} | |
# Example #### | |
# load in packages | |
library(nlsLoop) # to install devtools::install_github("padpadpadpad/nlsLoop") | |
library(nlstools) # to install install.packages("nlstools") | |
# load in data | |
data('Chlorella_TRC') | |
head(Chlorella_TRC) | |
# run nlsLoop | |
fits <- nlsLoop(ln.rate ~ schoolfield.high(ln.c, Ea, Eh, Th, temp = K, Tc = 20), | |
data = Chlorella_TRC, | |
tries = 500, | |
id_col = 'curve_id', | |
param_bds = c(-30, 30, 0, 10, 0, 30, 245, 385), | |
r2 = 'Y', | |
supp.errors = 'Y', | |
AICc = 'Y', | |
na.action = na.omit, | |
lower = c(ln.c=-10, Ea=0, Eh=0, Th=0)) | |
# calculate confidence intervals | |
confidence_intervals <- plyr::ldply(unique(Chlorella_TRC$curve_id), | |
confint_nlsLoop, | |
fits$params, | |
Chlorella_TRC, | |
'curve_id', | |
fits$formula, | |
na.action = na.omit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment