#####获得帮助##### #关于函数的帮助 help(glm) ?glm #关于操作符的帮助 ?`+` #查看帮助文档中例子 example(glm) #搜索主题 help.search("regression") ??regression #library函数的帮助选项 library(help="grDevices") #通过vignette查看指南文档,通过实际操作没反应 vignette("utils") #已加载包的指南文件 vignette(all=FALSE) #已安装包的指南文件 vignette(all=TRUE) ######R包##### #类似于java中类 #列举本地库中R包 getOption("defaultPackags") #已加载R包清单 (.packages()) #已安装的R包清单 (.packages(all.available=TRUE)) library() #加载R包 library(rpart) #搜索R包资源库:CRAN和Bioconductor #可以通过界面也可以通过命令,建议通过界面的菜单来操作 #R控制台来安装R包 install.packages(c("tree","maptree")) #移除R包 remove.packages(c("tree","maptree"))
输出
> #####获得帮助##### > help(glm) starting httpd help server ... done > ?glm > ?`+` > example(glm) glm> ## Dobson (1990) Page 93: Randomized Controlled Trial : glm> counts <- c(18,17,15,20,10,20,25,13,12) glm> outcome <- gl(3,1,9) glm> treatment <- gl(3,3) glm> print(d.AD <- data.frame(treatment, outcome, counts)) treatment outcome counts 1 1 1 18 2 1 2 17 3 1 3 15 4 2 1 20 5 2 2 10 6 2 3 20 7 3 1 25 8 3 2 13 9 3 3 12 glm> glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) glm> anova(glm.D93) Analysis of Deviance Table Model: poisson, link: log Response: counts Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev NULL 8 10.5814 outcome 2 5.4523 6 5.1291 treatment 2 0.0000 4 5.1291 glm> ## No test: glm> summary(glm.D93) Call: glm(formula = counts ~ outcome + treatment, family = poisson()) Deviance Residuals: 1 2 3 4 5 6 7 -0.67125 0.96272 -0.16965 -0.21999 -0.95552 1.04939 0.84715 8 9 -0.09167 -0.96656 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 3.045e+00 1.709e-01 17.815 <2e-16 *** outcome2 -4.543e-01 2.022e-01 -2.247 0.0246 * outcome3 -2.930e-01 1.927e-01 -1.520 0.1285 treatment2 8.717e-16 2.000e-01 0.000 1.0000 treatment3 4.557e-16 2.000e-01 0.000 1.0000 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for poisson family taken to be 1) Null deviance: 10.5814 on 8 degrees of freedom Residual deviance: 5.1291 on 4 degrees of freedom AIC: 56.761 Number of Fisher Scoring iterations: 4 glm> ## End(No test) glm> glm> ## No test: glm> ## an example with offsets from Venables & Ripley (2002, p.189) glm> utils::data(anorexia, package = "MASS") glm> anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt), glm+ family = gaussian, data = anorexia) glm> summary(anorex.1) Call: glm(formula = Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data = anorexia) Deviance Residuals: Min 1Q Median 3Q Max -14.1083 -4.2773 -0.5484 5.4838 15.2922 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 49.7711 13.3910 3.717 0.000410 *** Prewt -0.5655 0.1612 -3.509 0.000803 *** TreatCont -4.0971 1.8935 -2.164 0.033999 * TreatFT 4.5631 2.1333 2.139 0.036035 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for gaussian family taken to be 48.69504) Null deviance: 4525.4 on 71 degrees of freedom Residual deviance: 3311.3 on 68 degrees of freedom AIC: 489.97 Number of Fisher Scoring iterations: 2 glm> ## End(No test) glm> glm> # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2) glm> clotting <- data.frame( glm+ u = c(5,10,15,20,30,40,60,80,100), glm+ lot1 = c(118,58,42,35,27,25,21,19,18), glm+ lot2 = c(69,35,26,21,18,16,13,12,12)) glm> summary(glm(lot1 ~ log(u), data = clotting, family = Gamma)) Call: glm(formula = lot1 ~ log(u), family = Gamma, data = clotting) Deviance Residuals: Min 1Q Median 3Q Max -0.04008 -0.03756 -0.02637 0.02905 0.08641 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.0165544 0.0009275 -17.85 4.28e-07 *** log(u) 0.0153431 0.0004150 36.98 2.75e-09 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for Gamma family taken to be 0.002446059) Null deviance: 3.51283 on 8 degrees of freedom Residual deviance: 0.01673 on 7 degrees of freedom AIC: 37.99 Number of Fisher Scoring iterations: 3 glm> summary(glm(lot2 ~ log(u), data = clotting, family = Gamma)) Call: glm(formula = lot2 ~ log(u), family = Gamma, data = clotting) Deviance Residuals: Min 1Q Median 3Q Max -0.05574 -0.02925 0.01030 0.01714 0.06371 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.0239085 0.0013265 -18.02 4.00e-07 *** log(u) 0.0235992 0.0005768 40.91 1.36e-09 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for Gamma family taken to be 0.001813354) Null deviance: 3.118557 on 8 degrees of freedom Residual deviance: 0.012672 on 7 degrees of freedom AIC: 27.032 Number of Fisher Scoring iterations: 3 glm> ## Not run: glm> ##D ## for an example of the use of a terms object as a formula glm> ##D demo(glm.vr) glm> ## End(Not run) glm> glm> > help.search("regression") > ??regression > library(help="grDevices") > vignette("affy") Warning message: vignette ‘affy’ not found > local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE) + if(nchar(pkg)) library(pkg, character.only=TRUE)}) > vignette("base") Warning message: vignette ‘base’ not found > vignette(all=FALSE) > vignette("utils") Warning message: vignette ‘utils’ not found > vignette(all=FALSE) > vignette('utils') Warning message: vignette ‘utils’ not found > vignette(utils) Error in vignette(utils) : object 'utils' not found > vignette(all=TRUE) > vignette(all=TRUE) > #搜索主题 > vignette('colorspace') Warning message: vignette ‘colorspace’ not found > vignette("colorspace") Warning message: vignette ‘colorspace’ not found > getOption("defaultPackags") NULL > local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE) + if(nchar(pkg)) library(pkg, character.only=TRUE)}) > getOption("defaultPackags") NULL > utils:::menuInstallLocal() Error in install.packages(choose.files("", filters = Filters[c("zip", : no packages were specified > utils:::menuInstallPkgs() Error in install.packages(NULL, .libPaths()[1L], dependencies = NA, type = type) : no packages were specified > local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE) + if(nchar(pkg)) library(pkg, character.only=TRUE)}) > > > > library(utils) > getOption("defaultPackags") NULL > (.packages()) [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" [7] "base" > vignette("utils") Warning message: vignette ‘utils’ not found > vignette(all=FALSE) > (.packages(all.available=TRUE)) [1] "base" "boot" "class" "cluster" [5] "codetools" "colorspace" "compiler" "datasets" [9] "dichromat" "digest" "foreign" "ggplot2" [13] "graphics" "grDevices" "grid" "gtable" [17] "KernSmooth" "labeling" "lattice" "MASS" [21] "Matrix" "methods" "mgcv" "munsell" [25] "nlme" "nnet" "parallel" "plyr" [29] "proto" "RColorBrewer" "Rcpp" "reshape2" [33] "Rlab" "rpart" "scales" "spatial" [37] "splines" "stats" "stats4" "stringr" [41] "survival" "tcltk" "tools" "utils" > library() > library(rpart) > (.packages()) [1] "rpart" "stats" "graphics" "grDevices" "utils" "datasets" [7] "methods" "base" > install.packages(c("tree","maptree")) trying URL 'http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.1/tree_1.0-35.zip' Content type 'application/zip' length 117524 bytes (114 Kb) opened URL downloaded 114 Kb trying URL 'http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.1/maptree_1.4-7.zip' Content type 'application/zip' length 115110 bytes (112 Kb) opened URL downloaded 112 Kb package ‘tree’ successfully unpacked and MD5 sums checked package ‘maptree’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\hui.qian\AppData\Local\Temp\Rtmpk3BoLc\downloaded_packages > #移除R包 > remove.package(c("tree","maptree")) Error: could not find function "remove.package" > #移除R包 > remove.packages(c("tree","maptree")) Removing packages from ‘D:/R/R-3.1.2/library’ (as ‘lib’ is unspecified) >