Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何表示二元t统计量?_R_T Test - Fatal编程技术网

R 如何表示二元t统计量?

R 如何表示二元t统计量?,r,t-test,R,T Test,问题是这样给出的: 读取文件diabetes.csv。有两个变量称为BMI和结果。变量结果只有两个值:0和1。对两个结果值的BMI标准偏差相同的假设进行非参数双样本检验 bmi <- diabetes$BMI bmi outcome <- diabetes$Outcome outcome n <- length(bmi) # tstat tstat <- ??? # Describe the population and draw synthetic samples

问题是这样给出的:

读取文件diabetes.csv。有两个变量称为BMI和结果。变量结果只有两个值:0和1。对两个结果值的BMI标准偏差相同的假设进行非参数双样本检验

bmi <- diabetes$BMI
bmi
outcome <- diabetes$Outcome
outcome

n <- length(bmi)

# tstat
tstat <- ???

# Describe the population and draw synthetic samples
f1 <- function()
{
    x <- c(bmi, outcome) 
    x <- sample(x)
    m1 <- sd(x[1:n])
    m2 <- sd(x[(n+1):length(x)])
    return(m1 - m2)
}

# Create sampling distribution
sdist <- replicate(10000, f1())
plot(density(sdist))

# Gap
gap <- abs(mean(sdist) - tstat)
abline(v = mean(sdist) + c(-1,1) * gap, col = "dark orange")
s1 <- sdist[sdist <(mean(sdist - gap)) | sdist >(mean(sdist + gap))]
pvalue <- length(s1) / length(sdist)
pvalue
bmi使用以下代码:

# Sort the table diabetes on accending order of Outcome to separate the BMI 
# values with outcome = 0 and BMI values with outcome = 1

diabetes = diabetes[order(diabetes$Outcome),]   
View(diabetes)

# Find the number of values with outcome = 0

n = length(which(diabetes$Outcome == 0)) 

# Find total number of rows 

l = length(diabetes$BMI)               

# Find BMI values to create the sample later on

g = diabetes$BMI                           

# Create function to take the values of BMI and shuffle it every time and
# to find the difference between the standard deviations

f1 = function()
{
  x = sample(g)             
  z = abs(sd(x[1:n]) - sd(x[(n+1):l]))
  return(z)
}

# Replicate the function several times

dist = replicate(100000,f1())          

# Plot density of distribution

plot(density(dist))                    

polygon(density(dist),col="green")


diabetes0 = diabetes[diabetes$Outcome == 0,]
diabetes1 = diabetes[diabetes$Outcome == 1,]

View(diabetes0)
View(diabetes1)

# Find the difference between standard deviation of BMI when outcome = 0 and 
# when outcome = 1

tstat = abs(sd(diabetes0$BMI) - sd(diabetes1$BMI))       

tstat

abline(v=tstat)                                           
rside = dist[dist>tstat]    


pvalue = length(rside)/length(dist)
pvalue