一、通过help或者搜索,大致了解函数solve的作用,并简要说明。 help(solve)
http://127.0.0.1:16377/library/base/html/solve.html
这是个解决方程式AX=b的函数,b可以是矢量,也可以是矩阵。
二、自己尝试运行教科书modern applied statistics with s 中第一章所列的R代码,在理解每
条语句功能的前提下,尽可能多的给出中文注释。 1.1 > 2+3 [1] 5
> sqrt(3/4)/(1/3-2/pi^2) [1] 6.626513
> library(MASS)在R软件加载MASS的加载包 > data(chem) #needed in R only列出chem的数据 > mean(chem)算chem的算术平方根 [1] 4.280417
> m<-mean(chem);v<-var(chem)/length(chem)<-在R中表示赋值的意思 > m/sqrt(v) [1] 3.958487 1.2
help(var)
http://127.0.0.1:29162/library/stats/html/cor.html > help(\
http://127.0.0.1:29162/library/base/html/Extract.html > ?\
http://127.0.0.1:29162/library/base/html/Extract.html
help()和?””都是帮助查询R中函数的命令,作用相同 1.3
> library(MASS) > ?help
http://127.0.0.1:29162/library/utils/html/help.html
> x<-rnorm(1000) > y<-rnorm(1000)
> truehist(c(x,y+3),nbins=25)
x,y赋值后,用truehist画出该图的柱形图
> ?truehist
http://127.0.0.1:27932/library/MASS/html/truehist.html > contour(dd<-kde2d(x,y)) 画出等高线图
> image(dd) 画出彩色的矩形图
seq(1,20,0.5) > w<-1+x/2
> y<-x+w*rnorm(x)
> dum<-data.frame(x,y,w) > rm(x,y,w)
> fm<-lm(y~x,data=dum) > summary(fm)
Summary是产生各种模拟函数的结果概要的通用函数。 Call:
lm(formula = y ~ x, data = dum)
Residuals:
Min 1Q Median 3Q Max -14.697 -3.213 -1.854 5.413 11.843
Coefficients:
Estimate Std. Error t value Pr(>|t|) (Intercept) 0.2635 2.1811 0.121 0.905 x 0.8732 0.1831 4.769 2.87e-05 *** ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 6.434 on 37 degrees of freedom
Multiple R-squared: 0.3807, Adjusted R-squared: 0.364 F-statistic: 22.75 on 1 and 37 DF, p-value: 2.871e-05
> fm1<-lm(y~x,data=dum,weight=1/w^2) > summary(fm1) Call:
lm(formula = y ~ x, data = dum, weights = 1/w^2)
Weighted Residuals:
Min 1Q Median 3Q Max -1.9974 -0.5827 -0.1706 0.7948 1.4978
Coefficients:
Estimate Std. Error t value Pr(>|t|) (Intercept) -1.8681 0.8780 -2.128 0.0401 * x 1.1080 0.1405 7.888 1.92e-09 *** ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.9534 on 37 degrees of freedom
Multiple R-squared: 0.6271, Adjusted R-squared: 0.617 F-statistic: 62.23 on 1 and 37 DF, p-value: 1.923e-09
> lrf<-loess(y~x,dum) loess是一个拟合函数 > attach(dum) > plot(x,y)
> lines(spline(x,fitted(lrf)),col=2) > abline(0,1,lty=3,col=3) > abline(fm,col=4)
> abline(fm1,lty=4,col=5)
> plot(fitted(fm),resid(fm),xlab=\> qqnorm(resid(fm)) > qqline(resid(fm))
library(MASS) data(hills) pairs(hills) attach(hills) plot(dist,time)
identify(dist,time,row.names(hills)) abline(lm(time~dist)) library(lqs)
abline(lm(time~dist),lty=3,col=4) detach()清除之前的数据