R Language Program

Source: Internet
Author: User

I believe a lot of the above I said the R language interested in it, share my information it. This is the R program for verifying the central limit theorem!




##### #验证: Regardless of which distribution the random variable was originally subjected to, as long as the sample size is large enough,

######## #其均数都会服从正态分布


# # #1. Normal Distribution # # #


A<-rnorm (10000,0,1) #生成一个数据量很大的正态分布的数据

x<-1:100 #生成一个向量用来存放样本均数的向量

A<-data.frame (a) #将这两个向量存放在数据框中

X<-data.frame (x)


Windows (1280,720);p ar (mfrow=c (2,2))

Plot (density (a$a), main = "This is the original distribution")

hist (A$a,main = "This is the original distribution")


For (i in 1:100) {#设置循环, loop over 100 samples and assign the calculated mean to the x variable in the data frame

C<-a[sample (Nrow (a), 1000),]

M=mean (c)

# print (m) #验证该循环可以正常工作

X$x[i]<-m

}

Plot (density (x$x), main = "This is the distribution of the mean of the sampled sample") #绘制抽取样本的均值的分布概率密度图

hist (A$a,main = "This is the distribution of the mean of the sampled sample")



# #将上面的程序加以改造 to verify other forms of distribution

# # # 2.0 Index Distribution # # # #

A<-rexp (100000,1) #生成一个数据量很大的指数分布的数据

x<-1:100 #生成一个用来存放样本均数的数据框

A<-data.frame (a) #将这两个向量存放在数据框中

X<-data.frame (x)


Windows (1280,720);p ar (mfrow=c)

Plot (density (a$a), main = "This is the original distribution")


For (i in 1:10000) {#设置循环, loop over 100 samples and assign the calculated mean to the x variable in the data frame

C<-a[sample (Nrow (a), 1000),]

M=mean (c)

# print (m) #验证该循环可以正常工作

X$x[i]<-m

}

Plot (density (x$x), main = "This is the distribution of the mean of the sampled sample") #绘制抽取样本的均值的分布概率密度图


# # # 3.0 T Distribution # # #


A<-rt (100,2) #生成一个数据量很大的t分布的数据

x<-1:1000 #生成一个用来存放样本均数的数据框

A<-data.frame (a) #将这两个向量存放在数据框中

X<-data.frame (x)


Windows (1280,720);p ar (mfrow=c)


Plot (density (a$a), main = "This is the original distribution") #查看原始数据的分布状态

For (i in 1:1000) {#设置循环, loop over 1000 samples and assign the calculated mean to the x variable in the data frame

C<-a[sample (Nrow (a), 10),]

M=mean (c)

# print (m) #验证该循环可以正常工作

X$x[i]<-m

}

Plot (density (x$x), main = "This is the distribution of the mean of the sampled sample") #绘制抽取样本的均值的分布概率密度图



# # #4.0 F Distribution # # # #


A<-RF (10000,1,5) #生成一个数据量很大的F分布的数据

x<-1:1000 #生成一个用来存放样本均数的数据框

A<-data.frame (a) #将这两个向量存放在数据框中

X<-data.frame (x)


Windows (1280,720);p ar (mfrow=c)


Plot (density (a$a), main = "This is the original distribution")

For (i in 1:1000) {#设置循环, loop over 1000 samples and assign the calculated mean to the x variable in the data frame

C<-a[sample (Nrow (a), 1000),]

M=mean (c)

# print (m) #验证该循环可以正常工作

X$x[i]<-m

}

Plot (density (x$x), main = "This is the distribution of the mean of the sampled sample") #绘制抽取样本的均值的分布概率密度图



# # #5.0 Chi Square Distribution # # # # #


A<-RCHISQ (100000,1) #生成一个数据量很大的卡方分布的数据

x<-1:1000 #生成一个用来存放样本均数的数据框

A<-data.frame (a) #将这两个向量存放在数据框中

X<-data.frame (x)


Windows (400,400);p ar (mfrow=c)


Plot (density (a$a), main = "This is a set of data that is presented in Chi-square distribution")

For (i in 1:1000) {#设置循环, loop over 100 samples and assign the calculated mean to the x variable in the data frame

C<-a[sample (Nrow (a), 1000),]

M=mean (c)

# print (m) #验证该循环可以正常工作

X$x[i]<-m

}

Plot (density (x$x), main = "This is the mean distribution of 1000 samples extracted from the data of the chi-square distribution")

#绘制抽取样本的均值的分布概率密度图


# # # 6.0 Function Solution # # # #

Myfun<-function (a) {

X<-1:100 #先生成一个1到100的序列, these values can be changed later, which is equivalent to overwriting the original value

X<-data.frame (x)

A<-data.frame (a)

For (i in 1:100) {#设置循环, loop over 100 samples and assign the calculated mean to the x variable in the data frame

C<-a[sample (Nrow (a), 1000),]

M=mean (c)

X$x[i]<-m

}

Windows (1280,720);p ar (mfrow=c (2,2))

Plot (density (a$a), main = "This is the original distribution")

hist (A$a,main = "This is the original distribution", col= ' Skyblue ')

Plot (density (x$x), main = "This is the distribution of the mean of the sampled sample")

hist (A$a,main = "This is the distribution of the mean of the sampled sample", Col= ' Skyblue ')

}


# # #6.1 Normal Distribution # # #

A<-rnorm (10000,0,1)

Myfun (a)

# # #6.2 Index Distribution # # # # #

B<-rexp (100000,1)

Myfun (b)

# # #6.3 T Distribution # # # #

C<-rt (1000,3)

Myfun (c)

# # #6.4 F Distribution # # # #

D<-RCHISQ (100000,1)

Myfun (d)





R Language Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.