这个问题是关于使用R-统计软件包的多个样本的置信区间.
我创建了一个矩阵normsample=matrix(rnorm(25*100,man-6,sd=3),25,100)。我也有这样的功能:
> CIfun <- function(x, alpha, x.var)
> {
> ## Computes a confidence interval for mu from X ~ N(mu, x.var)
> ## x is a vector containing observations from X
> ## 1-alpha is the desired confidence level
> ## x.var is the assumed known variance for X
> n <- length(x) # find the number of elements in x
> x.mean <- mean(x) # calculate the mean of x
> z <- qnorm(1-alpha/2) # appropriate z-value
> lo <- x.mean - z*sqrt(x.var/n) # lower bound
> hi <- x.mean + z*sqrt(x.var/n) # upper bound
> return(c(lo, hi)) # return confidence interval as a vector
> }现在,我被要求使用"apply“函数和CIfun来创建一个名为meanconf的2x100矩阵。ith列应该包含对于尺寸为25的ith样本的μ的90%置信区间,第一行的下界和第二行的上界。我能理解他们要我做什么,但我不知道如何创建这个矩阵。有什么想法吗?(我使用了我的CIfun并应用函数,但是我得到了整个矩阵的置信区间的值(因为x是一个矩阵)。但是我想要计算矩阵的每个条目的置信区间)
发布于 2014-11-07 12:54:51
这可能有助于:
Reduce(`+`,lapply(lst, function(x) #apply CIfun over individual list elements
apply(x, 2, CIfun, 0.10, 9)))/length(lst)
#get the mean数据
set.seed(42) #set a seed
lst <- lapply(1:10, function(i) #crete a list of matrices
matrix(rnorm(25*100, mean=6, sd=3), 25, 100))https://stackoverflow.com/questions/26798509
复制相似问题