请修改下面的代码使它看起来不一样,注意将变量命名更换:xiti6<-function(N){ c=c(0:0) j=1 repeat{ a=runif(1000,0,1) b=0 for (i in 1:1000) { b=a[i]+b if(b>1){ c[j]=i j=j+1 break } i=i+1 } if(j>N) break } EK=mean(c) BZC=(sum((c-EK)^2))/(N-1) print(EK) #第二问平均值估计e print(sqrt(BZC)) #第三问M的标准差 #95%的置信区间为 t.test(c) } xiti6(10000)
时间: 2024-04-28 20:26:29 浏览: 69
xiti6_new <- function(num_samples){
sample_vector <- c()
counter <- 1
repeat{
random_nums <- runif(1000, 0, 1)
sample_sum <- 0
for (i in 1:1000){
sample_sum <- sample_sum + random_nums[i]
if(sample_sum > 1){
sample_vector[counter] <- i
counter <- counter + 1
break
}
}
if(counter > num_samples){
break
}
}
mean_estimate <- mean(sample_vector)
std_error <- sqrt(var(sample_vector) / num_samples)
lower_bound <- mean_estimate - qt(0.975, df = num_samples-1) * std_error
upper_bound <- mean_estimate + qt(0.975, df = num_samples-1) * std_error
cat("95% confidence interval for the mean: (", round(lower_bound, 4), ", ", round(upper_bound, 4), ")\n")
}
xiti6_new(10000)
阅读全文