日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

R语言第六讲 数据的统计分析

發(fā)布時間:2024/7/5 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 R语言第六讲 数据的统计分析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

基本命令練習(xí)

? ? ? ? ? ?下面的代碼涵蓋了一些分析數(shù)據(jù)常用的一些R語言的命令:

#基本向量、矩陣的一般操作 x <- c(1,3,2,5) x x = c(1,6,2) x y = c(1,4,3) length(x) length(y) x+y ls() rm(x,y) ls() rm(list=ls()) ?matrix x=matrix(data=c(1,2,3,4), nrow=2, ncol=2) x x=matrix(c(1,2,3,4),2,2) matrix(c(1,2,3,4),2,2,byrow=TRUE) sqrt(x) x^2 x=rnorm(50) y=x+rnorm(50,mean=50,sd=.1) cor(x,y) set.seed(1303)#代碼產(chǎn)生完全相同的一組隨機數(shù) # 用于設(shè)定隨機數(shù)種子,一個特定的種子可以產(chǎn)生一個特定的偽隨機序列,這個函數(shù)的主要目的, # 是讓你的模擬能夠可重復(fù)出現(xiàn),因為很多時候我們需要取隨機數(shù),但這段代碼再跑一次的時候, # 結(jié)果就不一樣了,如果需要重復(fù)出現(xiàn)同樣的模擬結(jié)果的話,就可以用set.seed()。在調(diào)試程序 # 或者做展示的時候,結(jié)果的可重復(fù)性是很重要的,所以隨機數(shù)種子也就很有必要。 rnorm(50) set.seed(3)#里面的參數(shù)可以是任意整數(shù) y=rnorm(100) mean(y) #計算均值 var(y) #計算方差 sqrt(var(y)) #計算開方 sd(y) #計算標(biāo)準(zhǔn)差 #分享一些作圖函數(shù) x=rnorm(100) y=rnorm(100) plot(x,y) plot(x,y,xlab="this is the x-axis",ylab="this is the y-axis",main="Plot of X vs Y") pdf("Figure.pdf")#用jpeg()函數(shù)繪制jpeg圖 plot(x,y,col="green") dev.off() x=seq(1,10) x x=1:10 x x=seq(-pi,pi,length=50) # contour()產(chǎn)生一個個等高線圖,表示三維數(shù)據(jù) y=x f=outer(x,y,function(x,y)cos(y)/(1+x^2)) contour(x,y,f)#?contour掌握更多用法,調(diào)整函數(shù)輸出 contour(x,y,f,nlevels=45,add=T) fa=(f-t(f))/2 contour(x,y,fa,nlevels=15) image(x,y,fa)#熱圖,能產(chǎn)生一個有顏色的圖 persp(x,y,fa)#產(chǎn)生一個三維圖 persp(x,y,fa,theta=30) persp(x,y,fa,theta=30,phi=20)#參數(shù)theta與phi控制觀看的角度 persp(x,y,fa,theta=30,phi=70) persp(x,y,fa,theta=30,phi=40) library(rgl) open3d() surface3d(x,y,fa,back="lines",color=terrain.colors(x^2))#繪制曲面 # Indexing Data dev.off A=matrix(1:16,4,4) A A[2,3] A[c(1,3),c(2,4)] A[1:3,2:4] A[1:2,] A[,1:2] A[1,] A[-c(1,3),] A[-c(1,3),-c(1,3,4)] dim(A) #觀看A向量的維數(shù)

R語言加載分析數(shù)據(jù)的幾種方法

data(Auto,package='ISLR')#輸出數(shù)據(jù)框格式 fix(Auto)#電子表格瀏覽數(shù)據(jù) Auto=read.table("Auto.data",header=T,na.strings="?") fix(Auto) Auto=read.csv("customer.csv",header=T,na.strings="?")#用指定的標(biāo)號作缺失標(biāo)記 fix(Auto)#挺好用的哇塞 dim(Auto) Auto[1:4,] Auto=na.omit(Auto)#簡單剔除缺失值所在的行 dim(Auto) names(Auto)# Additional Graphical and Numerical Summariesplot(cylinders, mpg) plot(Auto$cylinders, Auto$mpg) attach(Auto) plot(cylinders, mpg) cylinders=as.factor(cylinders)#將定量的變量轉(zhuǎn)換為定性變量 plot(cylinders, mpg) plot(cylinders, mpg, col="red") plot(cylinders, mpg, col="red", varwidth=T) plot(cylinders, mpg, col="red", varwidth=T,horizontal=T) plot(cylinders, mpg, col="red", varwidth=T, xlab="cylinders", ylab="MPG") #如果繪制在x軸上的變量是定性的,箱線圖(boxplot)將自動通過plot函數(shù)產(chǎn)生:數(shù)據(jù)沒有,需作驗證 hist(mpg) hist(mpg,col=2) hist(mpg,col=2,breaks=15) pairs(Auto) pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto) plot(horsepower,mpg) identify(horsepower,mpg,name)#交互指定顯示某個變量值 summary(Auto) summary(mpg)

最后保存當(dāng)前的工作空間,以便下次加載到這次的工作狀態(tài)

savehistory()loadhistory()

R語言分析數(shù)據(jù)

? 給出college.csv數(shù)據(jù)中的內(nèi)容,college.csv CSDN無法加載,若有小伙伴需要,可以留言哦(*^_^*)

college = read.csv("data//college.csv")#加載數(shù)據(jù) #用ecel表格方式打開查看某數(shù)據(jù) fix(college) #給college數(shù)據(jù)集中每一行數(shù)據(jù)命名 rownames(college) = college[,1] #刪除掉college數(shù)據(jù)中的第一列數(shù)據(jù) college = college[,-1]fix(college) summary(college)X Private Apps Abilene Christian University: 1 No :212 Min. : 81 Adelphi University : 1 Yes:565 1st Qu.: 776 Adrian College : 1 Median : 1558 Agnes Scott College : 1 Mean : 3002 Alaska Pacific University : 1 3rd Qu.: 3624 Albertson College : 1 Max. :48094 (Other) :771 Accept Enroll Top10perc前10名 Top25perc Min. : 72 Min. : 35 Min. : 1.00 Min. : 9.0 1st Qu.: 604 1st Qu.: 242 1st Qu.:15.00 1st Qu.: 41.0 Median : 1110 Median : 434 Median :23.00 Median : 54.0 Mean : 2019 Mean : 780 Mean :27.56 Mean : 55.8 3rd Qu.: 2424 3rd Qu.: 902 3rd Qu.:35.00 3rd Qu.: 69.0 Max. :26330 Max. :6392 Max. :96.00 Max. :100.0 F.Undergrad P.Undergrad Outstate Room.Board Min. : 139 Min. : 1.0 Min. : 2340 Min. :1780 1st Qu.: 992 1st Qu.: 95.0 1st Qu.: 7320 1st Qu.:3597 Median : 1707 Median : 353.0 Median : 9990 Median :4200 Mean : 3700 Mean : 855.3 Mean :10441 Mean :4358 3rd Qu.: 4005 3rd Qu.: 967.0 3rd Qu.:12925 3rd Qu.:5050 Max. :31643 Max. :21836.0 Max. :21700 Max. :8124 Books Personal PhD Terminal Min. : 96.0 Min. : 250 Min. : 8.00 Min. : 24.0 1st Qu.: 470.0 1st Qu.: 850 1st Qu.: 62.00 1st Qu.: 71.0 Median : 500.0 Median :1200 Median : 75.00 Median : 82.0 Mean : 549.4 Mean :1341 Mean : 72.66 Mean : 79.7 3rd Qu.: 600.0 3rd Qu.:1700 3rd Qu.: 85.00 3rd Qu.: 92.0 Max. :2340.0 Max. :6800 Max. :103.00 Max. :100.0 S.F.Ratio perc.alumni Expend Grad.Rate Min. : 2.50 Min. : 0.00 Min. : 3186 Min. : 10.00 1st Qu.:11.50 1st Qu.:13.00 1st Qu.: 6751 1st Qu.: 53.00 Median :13.60 Median :21.00 Median : 8377 Median : 65.00 Mean :14.09 Mean :22.74 Mean : 9660 Mean : 65.46 3rd Qu.:16.50 3rd Qu.:31.00 3rd Qu.:10830 3rd Qu.: 78.00 Max. :39.80 Max. :64.00 Max. :56233 Max. :118.00 pairs(college[,1:10]) #取college 1到10列的變量,兩兩做散點圖

?由上圖,可以看看兩兩變量之間到底有沒有函數(shù)關(guān)系。

總結(jié)

以上是生活随笔為你收集整理的R语言第六讲 数据的统计分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。