R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("zicounts-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('zicounts') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "enzinb" > > ### * enzinb > > flush(stderr()); flush(stdout()) > > ### Name: ezinb > ### Title: The expected value of the censored zero-inflated negative > ### binomial model > ### Aliases: ezinb > ### Keywords: models > > ### ** Examples > > ## load the artificial 'teeth' data > data(teeth) > names(teeth) [1] "dmft" "decid" "age" "gender" > > ## fit zero-inflated negative binomial regression model > zinb.cens <- zicensor(resp = dmft~., upper = decid~.,x =~gender + age,z =~gender + age, data=teeth) > zinb.cens Censored Zero-Inflated Negative Binomial Call: Formula = ( dmft , decid ) ~ gender + age | gender + age Subscripts: -- x: coefficients of censored negative binomial part -- z: coefficients of censored zero-inflated part Coefficients: (Intercept)x genderx agex (Intercept)z genderz 1.424919 -0.004769 -0.047181 -3.590246 0.284743 agez log(tau) 0.407158 2.927695 Degrees of Freedom: 193 Deviance: 571.6 AIC: 585.6 BIC: 608.7 Number of iterations BFGS is called before convergence: 1 > > ## expected value of the censored zinb model > ## uncomment to run > #zinb.e <- ezinb(coeff=zinb.cens$coeff,resp = dmft~., upper = decid~., > # x =~gender + age,z =~gender + age, data=teeth) > #zinb.e > > > > cleanEx(); ..nameEx <- "summary.zinb" > > ### * summary.zinb > > flush(stderr()); flush(stdout()) > > ### Name: summary.zinb > ### Title: Printing summary of the (censored) zinb regression model > ### Aliases: summary.zinb summary.czinb print.zinb print.czinb > ### prnt.zinb.int > ### Keywords: models > > ### ** Examples > > ## See help for ?zicounts or ?zicensor > > > > cleanEx(); ..nameEx <- "teeth" > > ### * teeth > > flush(stderr()); flush(stdout()) > > ### Name: teeth > ### Title: A simulated data set to demonstrate the zicounts package > ### Aliases: teeth > ### Keywords: models > > ### ** Examples > > ## See ?zicouts, zicensor > > > > cleanEx(); ..nameEx <- "zicensor" > > ### * zicensor > > flush(stderr()); flush(stdout()) > > ### Name: zicensor > ### Title: Fitting classical and zero-inflated count regression models > ### Aliases: zicensor > ### Keywords: models > > ### ** Examples > > ## load the artificial 'teeth' data > data(teeth) > names(teeth) [1] "dmft" "decid" "age" "gender" > > ## a) fit a Poisson regression model > pois.cens1 <- zicensor(parm=c(-2,-2,1),resp = dmft~., upper = decid~.,x =~gender + age, + data=teeth, distr = "Poisson") > pois.cens1 Censored Poisson Call: Formula = ( dmft , decid ) ~ gender + age Coefficients: (Intercept)x genderx agex 1.8615 -0.1125 -0.1599 Degrees of Freedom: 197 Deviance: 665.6 AIC: 671.6 BIC: 681.5 Number of iterations BFGS is called before convergence: 1 > # fit for boys only -- using 'sub' > pois.boys <- zicensor(resp = dmft~., upper = decid~.,x =~age, data=teeth, distr = "Poisson", + sub=expression(gender==0)) > pois.boys Censored Poisson Call: Formula = ( dmft , decid ) ~ age Coefficients: (Intercept)x agex 0.794384 -0.007336 Degrees of Freedom: 74 Deviance: 254.8 AIC: 258.8 BIC: 263.5 Number of iterations BFGS is called before convergence: 1 > > ## b) fit zero-inflated Poisson regression model > zip.cens <- zicensor(resp = dmft~., upper = decid~.,x =~gender + age,z =~gender + age, + data=teeth, distr = "ZIP") > zip.cens Censored Zero-Inflated Poisson Call: Formula = ( dmft , decid ) ~ gender + age | gender + age Subscripts: -- x: coefficients of censored Poisson part -- z: coefficients of censored zero-inflated part Coefficients: (Intercept)x genderx agex (Intercept)z genderz 1.39837 0.01205 -0.05841 -3.16086 0.72597 agez 0.28842 Degrees of Freedom: 194 Deviance: 577.2 AIC: 589.2 BIC: 609 Number of iterations BFGS is called before convergence: 1 > #summary(zip.cens) > > ## c) fit negative binomial regression model > nb.cens <- zicensor(resp = dmft~., upper = decid~.,x =~gender + age,data=teeth, distr = "NB") > nb.cens Censored Negative Binomial Call: Formula = ( dmft , decid ) ~ gender + age Coefficients: (Intercept)x genderx agex log(tau) 2.0229 -0.1108 -0.1860 0.1673 Degrees of Freedom: 196 Deviance: 594.8 AIC: 602.8 BIC: 616 Number of iterations BFGS is called before convergence: 1 > #summary(nb.cens) > > ## d) fit zero-inflated negative binomial regression model > zinb.cens <- zicensor(resp = dmft~., upper = decid~.,x =~gender + age,z =~gender + age, data=teeth) > zinb.cens Censored Zero-Inflated Negative Binomial Call: Formula = ( dmft , decid ) ~ gender + age | gender + age Subscripts: -- x: coefficients of censored negative binomial part -- z: coefficients of censored zero-inflated part Coefficients: (Intercept)x genderx agex (Intercept)z genderz 1.424919 -0.004769 -0.047181 -3.590246 0.284743 agez log(tau) 0.407158 2.927695 Degrees of Freedom: 193 Deviance: 571.6 AIC: 585.6 BIC: 608.7 Number of iterations BFGS is called before convergence: 1 > #summary(zinb.cens) > > > > cleanEx(); ..nameEx <- "zicounts" > > ### * zicounts > > flush(stderr()); flush(stdout()) > > ### Name: zicounts > ### Title: Fitting classical and zero-inflated count regression models > ### Aliases: zicounts > ### Keywords: models > > ### ** Examples > > ## load the artificial 'teeth' data > data(teeth) > names(teeth) [1] "dmft" "decid" "age" "gender" > > ## a) fit a Poisson regression model > pois.zc1 <- zicounts(parm=c(-2,-2,1),resp = dmft~.,x =~gender + age, data=teeth, distr = "Poisson") > pois.zc1 # starting values specified Poisson Call: Formula = dmft ~ gender + age Coefficients: (Intercept)x genderx agex 1.9444 -0.1119 -0.1947 Degrees of Freedom: 197 Deviance: 814.9 AIC: 820.9 BIC: 830.8 Number of iterations BFGS is called before convergence: 1 > > pois.zc2 <- zicounts(resp = dmft~.,x =~gender + age, data=teeth, distr = "Poisson") > pois.zc2 # starting values estimated from data Poisson Call: Formula = dmft ~ gender + age Coefficients: (Intercept)x genderx agex 1.9444 -0.1119 -0.1947 Degrees of Freedom: 197 Deviance: 814.9 AIC: 820.9 BIC: 830.8 Number of iterations BFGS is called before convergence: 1 > > # compare with glm > pois.glm <- glm(dmft~gender + age, family=poisson,data=teeth) > pois.glm Call: glm(formula = dmft ~ gender + age, family = poisson, data = teeth) Coefficients: (Intercept) gender age 1.9444 -0.1119 -0.1947 Degrees of Freedom: 199 Total (i.e. Null); 197 Residual Null Deviance: 505.2 Residual Deviance: 502.3 AIC: 820.9 > > # fit for boys only -- using 'sub' > pois.girl <- zicounts(resp = dmft~.,x =~age, data=teeth, distr = "Poisson",sub=expression(gender==0)) > pois.girl # starting values estimated from data Poisson Call: Formula = dmft ~ age Coefficients: (Intercept)x agex 1.03854 -0.06515 Degrees of Freedom: 74 Deviance: 317.4 AIC: 321.4 BIC: 326 Number of iterations BFGS is called before convergence: 1 > > # Poisson with offset > pois.zc3 <- zicounts(resp = dmft~.,x =~gender + age, offset= expression(log(decid + 1)), data=teeth, distr = "Poisson") > pois.zc3 Poisson Call: Formula = dmft ~ gender + age (with offest) Coefficients: (Intercept)x genderx agex 0.06165 -0.05201 -0.10235 Degrees of Freedom: 197 Deviance: 501.6 AIC: 507.6 BIC: 517.5 Number of iterations BFGS is called before convergence: 1 > > # compare with glm > pois.glm2 <- glm(dmft~gender + age, offset= log(decid + 1), family=poisson,data=teeth) > pois.glm2 Call: glm(formula = dmft ~ gender + age, family = poisson, data = teeth, offset = log(decid + 1)) Coefficients: (Intercept) gender age 0.06165 -0.05201 -0.10235 Degrees of Freedom: 199 Total (i.e. Null); 197 Residual Null Deviance: 189.7 Residual Deviance: 189 AIC: 507.6 > > ## b) fit zero-inflated Poisson regression model > zip.zc <- zicounts(resp = dmft~.,x =~gender + age,z =~gender + age, data=teeth, distr = "ZIP") > zip.zc Zero-Inflated Poisson Call: Formula = dmft ~ gender + age | gender + age Subscripts: -- x: coefficients of Poisson part -- z: coefficients of zero-inflated part Coefficients: (Intercept)x genderx agex (Intercept)z genderz 1.133932 -0.039455 -0.004717 -3.519678 0.171604 agez 0.446568 Degrees of Freedom: 194 Deviance: 668.4 AIC: 680.4 BIC: 700.2 Number of iterations BFGS is called before convergence: 1 > #summary(zip.zc) > > ## c) fit negative binomial regression model > nb.zc <- zicounts(resp = dmft~.,x =~gender + age,data=teeth, distr = "NB") > nb.zc Negative Binomial Call: Formula = dmft ~ gender + age Coefficients: (Intercept)x genderx agex log(tau) 2.0640 -0.1172 -0.2113 -0.2556 Degrees of Freedom: 196 Deviance: 704.4 AIC: 712.4 BIC: 725.6 Number of iterations BFGS is called before convergence: 1 > #summary(nb.zc) > > ## d) fit zero-inflated negative binomial regression model > zinb.zc <- zicounts(resp = dmft~.,x =~gender + age,z =~gender + age, data=teeth) > zinb.zc Zero-Inflated Negative Binomial Call: Formula = dmft ~ gender + age | gender + age Subscripts: -- x: coefficients of of negative binomial part -- z: coefficients of zero-inflated part Coefficients: (Intercept)x genderx agex (Intercept)z genderz 1.125896 -0.040113 -0.004892 -3.584960 0.172345 agez log(tau) 0.452557 3.421015 Degrees of Freedom: 193 Deviance: 668 AIC: 682 BIC: 705.1 Number of iterations BFGS is called before convergence: 1 > #summary(zinb.zc) > > > > ### *