nls - Fitting Generalized Nonlinear Model in R -
nls - Fitting Generalized Nonlinear Model in R -
i want fit next generalized nonlinear model: probit(g)=k+1/sigma*(temp-t0)*time. naive model, created probits(g) qnorm(g) , fitted nonlinear model. want fit nonlinear model logit link similar glm function in r. highly appreciate if help me fit such generalized nonlinear model logit link in r. in advance help.
data <- structure(list(temp = c(23l, 23l, 23l, 23l, 23l, 23l, 23l, 23l, 27l, 27l, 27l, 27l, 27l, 27l, 33l, 33l, 33l, 33l, 33l, 33l, 33l, 35l, 35l, 35l, 35l, 35l), time = c(144l, 168l, 192l, 216l, 240l, 264l, 288l, 312l, 120l, 144l, 168l, 192l, 216l, 240l, 72l, 96l, 120l, 144l, 168l, 192l, 216l, 96l, 120l, 144l, 168l, 192l), g = c(15, 25.5, 27, 28, 28.5, 39.5, 41.5, 43, 13, 21.5, 29.5, 30.5, 32.5, 35, 13.5, 28, 32.5, 33.5, 35, 39.5, 42, 6.5, 30, 39.5, 57, 58.5 )), .names = c("temp", "time", "g"), class = "data.frame", row.names = c(na, -26l)) data$germrate <- 1/data$time data$probits <- qnorm(p=data$g/100) # probits fm1 <- nls( formula= probits ~ k+1/sigma*(temp-t0)*time , data=data , start=list(k=1, sigma=2, t0=2) #, algorithm= "port" ) fm1summary <- summary(fm1) fm1coef <- summary(fm1)$coef
you can fit type of model using gnm bundle generalized nonlinear models. takes bit of work, gnm uses pre-defined functions of class "nonlin" specify nonlinear terms in model , ones provided bundle insufficient specify arbitrary nonlinear function. possible define custom "nonlin" function utilize gnm.
in model, k linear parameter, need worry sec term. can specified via next "nonlin"function
customnonlin <- function(temp, time){ list(predictors = list(sigma = 1, t0 = 1), variables = list(substitute(temp), substitute(time)), term = function(predlabels, varlabels) { sprintf("1/%s * (%s - %s) * %s", predlabels[1], varlabels[1], predlabels[2], varlabels[2]) }) } class(customnonlin) <- "nonlin" in returned list,
predictors specifies sigma , t0 predictors single intercept term (i.e. individual parameters). variables specifies there 2 variables, supplied user via temp , time arguments. term specifies function create deparsed mathematical look of term, given labels predictors , variables. more detail on "nonlin" functions can found in section 3.5 of gnm vignette.
now can seek fitting model follows
mod1 <- gnm(cbind(g, 100 - g) ~ customnonlin(temp, time), family = binomial, info = data, start = c(1, 2, 2)) note that, in glm, intercept added formula default, here represent k. although starting values far solution, gnm convergence criteria met @ point, algorithm not perform iterations. in case improve starting estimate of sigma required gnm converge more sensible
mod2 <- gnm(cbind(g, 100 - g) ~ customnonlin(temp, time), family = binomial, info = data, start = c(1, 2000, 2)) mod2 call: gnm(formula = cbind(g, 100 - g) ~ customnonlin(temp, time), family = binomial, info = data, start = c(1, 2000, 2)) coefficients: (intercept) sigma t0 -2.589 1915.602 8.815 deviance: 53.53157 pearson chi-squared: 49.91347 residual df: 23 actually is possible specify model using mult function provided gnm, long don't mind re-parameterising model:
mod3 <- gnm(cbind(g, 100 - g) ~ mult(1, 1 + offset(temp), offset(time)), family = binomial, info = data, start = c(1, 1/2000, -2)) mod3 call: gnm(formula = cbind(g, 100 - g) ~ mult(1, offset(temp) + 1, offset(time)), family = binomial, info = data, start = c(1, 1/2000, -2)) coefficients: (intercept) -2.588874 mult(., 1 + offset(temp), offset(time)). 0.000522 mult(1, . + offset(temp), offset(time)). -8.815152 deviance: 53.53157 pearson chi-squared: 49.91347 residual df: 23 edit
the function of parameters specified in term component of list returned customnonlin, can see via
customnonlin(temp, time)$term(c("sigma", "t0"), c("temp", "time")) "1/sigma * (temp - t0) * time" so if want alter functional form, need modify term function. if want add/remove parameters, need modify list in predictors component. if new term requires add/remove variables, modify variables component.
r nls gnm
Comments
Post a Comment