R:编码为什么在结果中显示0.00 [英] R: coding why show 0.00 in result
问题描述
我的模拟目标是在几个因素组合下评估测试的类型1错误率。
My aims of this simulation is to evaluate the type 1 error rate of the tests under several combination of factors.
-
样本量 - (10,10),(10,25),(25,25),(25,50),(25,100),50,25),(50,100),(100,25),(100,100)
sample sizes-(10,10),(10,25),(25,25),(25,50),(25,100),50,25),(50,100), (100,25),(100,100)
标准差比率 - (1.00,1.50,2.00,2.50,3.00和3.50)
standard deviation ratio- (1.00, 1.50, 2.00, 2.50, 3.00 and 3.50)
具有不等偏度和相等偏度的伽玛分布的分布
distribution of gamma distribution with unequal skewness and equal skewness
所涉及的2个样本测试是汇总方差t检验和welch t test和mann whitney test。我尝试使用上述因素组合修改代码。
The 2 sample test involved are pooled variance t test and welch t test and mann whitney test. I tried to modified a code by using the above combination of factors.
########################################
#for normal distribution setup
# to ensure the reproducity of the result
#(here we declare the random seed generator)
set.seed(1)
## Put the samples sizes into matrix then use a loop for sample sizes
sample_sizes<-matrix(c(10,10,10,25,25,25,25,50,25,100,50,25,50,100,100,25,100,100),
nrow=2)
#create vector to combine all std deviations
sds<-matrix(c(4,4,6,4,8,4,10,4,12,4,14,4),nrow=2)
sd1<-c(4,6,8,10,12)
sd2<-c(4,4,4,4,4)
sds2<-rep(sd2,each=9)
##(use expand.grid)to create a data frame from combination of data
ss_sds1<- expand.grid(sample_sizes[2,], sd1)
#create a matrix combining the fifty four cases of combination of ss and sds
all_combine <- cbind(rep(sample_sizes[1,], 5), ss_sds1,sds2)
# name the column by sample samples 1 and 2 and standard deviation
colnames(all_combine) <- c("m", "n", "sds1","sds2")
#number of simulations
nSims<-10000
#set significance level,alpha for the whole simulation
alpha<-0.05
#set up matrix for storing data from simulation
#set nrow =nsims because wan storing every p-value simulated
matrix1_equal <-matrix(0,nrow=nSims,ncol=9)
matrix4_unequal<-matrix(0,nrow=nSims,ncol=9)
matrix7_mann <-matrix(0,nrow=nSims,ncol=9)
#set up vector for storing data from the three tests (nrow for all_combine=45)
equal1 <- unequal4<- mann7 <- rep(0, nrow(all_combine))
# this loop steps through the all_combine matrix
for(ss in 1:nrow(all_combine))
{
#generate samples from the first column and second column
m<-all_combine[ss,1]
n<-all_combine[ss,2]
for (sim in 1:nSims)
{
#generate random samples from 2 normal distribution
x<-rnorm(m,5,all_combine[ss,3])
y<-rnorm(n,5,4)
#extract p-value out and store every p-value into matrix
matrix1_equal[sim,1]<-t.test(x,y,var.equal=TRUE)$p.value
matrix4_unequal[sim,4]<-t.test(x,y,var.equal=FALSE)$p.value
matrix7_mann[sim,7] <-wilcox.test(x,y)$p.value
}
##store the result
equal1[ss]<- mean(matrix1_equal[,1]<=alpha)
unequal4[ss]<-mean(matrix4_unequal[,4]<=alpha)
mann7[ss]<- mean(matrix7_mann[,7]<=alpha)
}
# combine results
nresult <- cbind(all_combine, equal1, unequal4, mann7)
save.image(file="normal.data")
我是R的新手,现在我已经完成了正态分发的代码,并且还需要再添加两个模拟关于你的伽马分布的分布唱歌如果其他......任何人都可以给出一些建议如何改变正常的分配。伽马分配?我现在正在坚持这一部分...
I am new in R , now i have completed a code in normal distribution and have to add on two more simulation on distribution of gamma distribution by using if else...can anyone pls give some advice how to change from normal distr. to gamma distr? I am stucking in this part right now...
帮助!!
上面的代码多次给我结果0.00,我已经多次检查它们但我没有发现任何错误。
请
HELP!! the code above gave me result 0.00 for several times, i check them for many times already and yet i did not spot any mistake. Please
推荐答案
这是我目前的编码..
This is my current coding..
########################################
#for normal distribution setup
# to ensure the reproducity of the result
#(here we declare the random seed generator)
set.seed(1)
## Put the samples sizes into matrix then use a loop for sample sizes
sample_sizes<-matrix(c(10,10,10,25,25,25,25,50,25,100,50,25,50,100,100,25,100,100),
nrow=2)
#create vector to combine all std deviations
sds<-matrix(c(4,4,6,4,8,4,10,4,12,4,14,4),nrow=2)
sd1<-c(4,6,8,10,12)
sd2<-c(4,4,4,4,4)
sds2<-rep(sd2,each=9)
##(use expand.grid)to create a data frame from combination of data
ss_sds1<- expand.grid(sample_sizes[2,], sd1)
#create a matrix combining the fifty four cases of combination of ss and sds
all_combine <- cbind(rep(sample_sizes[1,], 5), ss_sds1,sds2)
# name the column by sample samples 1 and 2 and standard deviation
colnames(all_combine) <- c("m", "n", "sds1","sds2")
#number of simulations
nSims<-10000
#set significance level,alpha for the whole simulation
alpha<-0.05
#set up matrix for storing data from simulation
#set nrow =nsims because wan storing every p-value simulated
matrix1_equal <-matrix(0,nrow=nSims,ncol=9)
matrix4_unequal<-matrix(0,nrow=nSims,ncol=9)
matrix7_mann <-matrix(0,nrow=nSims,ncol=9)
#set up vector for storing data from the three tests (nrow for all_combine=45)
equal1 <- unequal4<- mann7 <- rep(0, nrow(all_combine))
# this loop steps through the all_combine matrix
for(ss in 1:nrow(all_combine))
{
#generate samples from the first column and second column
m<-all_combine[ss,1]
n<-all_combine[ss,2]
for (sim in 1:nSims)
{
#generate random samples from 2 normal distribution
x<-rnorm(m,5,all_combine[ss,3])
y<-rnorm(n,5,4)
#extract p-value out and store every p-value into matrix
matrix1_equal[sim,1]<-t.test(x,y,var.equal=TRUE)$p.value
matrix4_unequal[sim,4]<-t.test(x,y,var.equal=FALSE)$p.value
matrix7_mann[sim,7] <-wilcox.test(x,y)$p.value
}
##store the result
equal1[ss]<- mean(matrix1_equal[,1]<=alpha)
unequal4[ss]<-mean(matrix4_unequal[,4]<=alpha)
mann7[ss]<- mean(matrix7_mann[,7]<=alpha)
}
# combine results
nresult <- cbind(all_combine, equal1, unequal4, mann7)
save.image(file="normal.data")
这篇关于R:编码为什么在结果中显示0.00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!