您好,欢迎访问三七文档
二、R1.下载安装运行环境:英文状态,以函数形式运行3.向量(1)输入①x-c(1,3,2,5)x[1]1325②x=c(1,6,2)x[1]162③x=c(1:10)x=1:10④x=seq(1,10)x=seq(-2*pi,2*pi,length=50)y=c(1,4,3)(2)长度length(x)[1]3length(y)[1]3(3)运算x,y的长度一样,可以进行运算。例如x+y,x-y,x*y,x/y,这些运算是分元素进行的。(4)显示和清除变量ls()%显示变量[1]xy%清除变量x,yrm(x,y)ls()character(0)rm(list=ls())%清除所有变量(5)保留小数位数①x=round(x,3)②sprintf(“%.10f”,0.25)4.矩阵(1)寻求帮助help(matrix)或?matrix(2)输入①x=matrix(data=c(1,2,3,4),nrow=2,ncol=2)x[,1][,2][1,]13[2,]24②x=matrix(c(1,2,3,4),2,2)③matrix(c(1,2,3,4),2,2,byrow=TRUE)[,1][,2][1,]12[2,]34④产生随机矩阵:x=rnom(%填行列数)扩展1:1-9按行、列排成3行3列的矩阵。x=matrix(data=c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3,byrow=TURE)x[,1][,2][,3][1,]123[2,]456[3,]789x=matrix(data=c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)%x[,1][,2][,3][1,]147[2,]258[3,]369扩展2:99个1按行排成3行33列的矩阵。y=matrix(data=c(1),nrow=3,ncol=3,byrow=TURE)(3)矩阵的运算x,y的行、列都一样,可以进行运算。例如x+y,x-y,x*y,x/y,这些运算是分元素进行的。高代中矩阵的乘法:x%*%y高代中矩阵的除法:x%*%solve(y)(4)元素的提取A=matrix(1:16,4,4)A[,1][,2][,3][,4][1,]15913[2,]261014[3,]371115[4,]481216①A[2,3][1]10②A[c(1,3),c(2,4)][,1][,2][1,]513[2,]715③A[1:3,2:4][,1][,2][,3][1,]5913[2,]61014[3,]71115④A[1:2,][,1][,2][,3][,4][1,]15913[2,]261014A[,1:2][,1][,2][1,]15[2,]26[3,]37[4,]48A[1,][1]15913⑤A[-c(1,3),][,1][,2][,3][,4][1,]261014[2,]481216A[-c(1,3),-c(1,3,4)][1]68⑥dim(A)[1]445.绘图(1)散点图x=rnorm(100)y=rnorm(100)plot(x,y)plot(x,y,xlab=thisisthex-axis,ylab=thisisthey-axis,main=PlotofXvsY)-2-1012-2-1012xy-2-1012-2-1012PlotofXvsYthisisthex-axisthisisthey-axis(2)保存图片①pdf类型pdf(Figure.pdf)plot(x,y,col=green)dev.off()nulldevice1②jpeg类型jpeg(Figure.jpeg)(3)等高线图y=xf=outer(x,y,function(x,y)cos(y)/(1+x^2))contour(x,y,f)%下面第一幅图contour(x,y,f,nlevels=45,add=T)%下面第二幅图fa=(f-t(f))/2contour(x,y,fa,nlevels=15)%下面第三幅图image(x,y,fa)%下面第四幅图-0.8-0.8-0.4-0.4-0.2-0.2000.20.40.60.8-3-2-10123-3-10123-0.8-0.8-0.4-0.4-0.2-0.2000.20.40.60.8-3-2-10123-3-10123-0.9-0.9-0.85-0.85-0.6-0.6-0.5-0.5-0.4-0.4-0.25-0.25-0.2-0.2-0.1-0.1-0.05-0.05000.050.050.10.150.20.250.350.40.450.50.60.70.8-0.4-0.4-0.3-0.3-0.2-0.2-0.1-0.100000.10.10.20.20.30.30.40.4-3-2-10123-3-10123-3-2-10123-3-2-10123xy(4)轮廓图persp(x,y,fa)%下面第一幅图persp(x,y,fa,theta=30)%下面第二幅图persp(x,y,fa,theta=30,phi=20)%下面第三幅图persp(x,y,fa,theta=30,phi=70)%下面第四幅图xyfaxyfaxyfaxyfa扩展1.在同一图中画出正弦与余弦曲线①x=seq(-2*pi,2*pi,length=40)y=sin(x)plot(x,y,xlim=c(-2*pi,2*pi),ylim=c(-1,1),col=red,lty=1,xlab=角度,ylab=数值,main=正弦余弦曲线)y=cos(x)lines(x,y,col=blue,lty=2)legend(topleft,inset=.05,c(sin,cos),lty=c(1,2),col=c(red,blue))②x=seq(-2*pi,2*pi,length=40)y=sin(x)plot(x,y,xlim=c(-2*pi,2*pi),ylim=c(-1,1),col=red,type=l,xlab=角度,ylab=数值,main=正弦余弦曲线)y=cos(x)lines(x,y,col=blue,lty=2)legend(topleft,inset=.05,c(sin,cos),lty=c(1,2),col=c(red,blue))-6-4-20246-1.0-0.50.00.51.0正弦余弦曲线角度数值sincos-6-4-20246-1.0-0.50.00.51.0正弦余弦曲线角度数值sincos①的结果②的结果注:type=p在图形中数据显示为点type=l在图形中数据显示为线type=b在图形中数据显示为点和连接线type=o在图形中数据点覆盖在线上type=h在图形中数据显示为从点到x轴的垂直线type=s在图形中数据显示为阶梯图type=n在图形中数据不显示拓展2.画马鞍面,并从不同视角观察。x=seq(-5,5,length=50)y=xz=outer(x,y,function(x,y)x*y)persp(x,y,z)persp(x,y,z,theta=30)persp(x,y,z,theta=50)persp(x,y,z,theta=30,phi=20)persp(x,y,z,theta=30,phi=70)persp(x,y,z,theta=30,phi=40)6.数据的输入、观察(1)数据的输入Auto=read.csv(Auto.csv)注:把Excel文件保存成.csv格式,并放在运行路径下,在运行上面命令加载数据。(2)数据的观察fix(Auto)(3)删除无数据的行Auto=na.omit(Auto)(4)查看数据的变量names(Auto)[1]mpgcylindersdisplacementhorsepower[5]weightaccelerationyearorigin[9]name(5)plot(cylinders,mpg)错误。没有发现目标cylinders。R不能知道Auto数据集的变量。解决办法:①plot(Auto$cylinders,Auto$mpg)②利用attach()函数:attach(Auto)plot(cylinders,mpg)(6)直方图hist(map)HistogramofmpgmpgFrequency1020304050020406080(7)产生矩阵散点图mpg380101.01038cylindersdisplacement1000horsepowerweight150010accelerationyear701.0origin1010015007000name7.单元线性回归{(x1,y1),······,(xn,yn)},n个样本点。其中)()(1,,y,x,xxiiiip。线性回归:εxβxββYpp110,估计:pp110xβˆxβˆβˆYˆ。求)ˆ,,ˆ(ˆ0p。(1)程序包加载:①工具栏中程序包→加载②工具栏中程序包→安装→加载(2)求解ˆfix(Boston)names(Boston)[1]crimzninduschasnoxrmage[8]disradtaxptratioblacklstatmedvlm.fit=lm(medv∼lstat,data=Boston)attach(Boston)lm.fit=lm(medv∼lstat)lm.fitCall:lm(formula=medv~lstat)Coefficients:(Intercept)lstat34.55-0.95summary(lm.fit)Call:lm(formula=medv∼lstat)Residuals:Min1QMedian3QMax-15.17-3.99-1.322.0324.50Coefficients:EstimateStd.ErrortvaluePr(|t|)(Intercept)34.55380.562661.42e-16***lstat-0.95000.0387-24.52e-16***---Signif.codes:0***0.001**0.01*0.05.0.11Residualstandarderror:6.22on504degreesoffreedomMultipleR-squared:0.544,AdjustedR-squared:0.543F-statistic:602on1and504DF,p-value:2e-16names(lm.fit)[1]coefficientsresidualseffects[4]rankfitted.valuesassign[7]qrdf.residualxlevels[10]calltermsmodelcoef(lm.fit)(Intercept)lstat34.55-0.95(3)检测confint(lm.fit)2.5%97.5%(Intercept)33.4535.659lstat-1.03-0.874(4)预测predict(lm.fit,data.frame(lstat=c(5,10,15)),interval=confidence)fitlwrupr129.8029.0130.60225.0524.4725.63320.3019.7320.87predict(lm.fit,data.frame(lstat=c(5,10,15)),interval=prediction)fitlwrupr129.8017.56642.04225.0512.82837.28320.308.07832.53
本文标题:R语言基础
链接地址:https://www.777doc.com/doc-5305593 .html