> rawdata[rawdata$group==1,][1,] 临床诊断 BIL Alb ALP ALT group 1 肝炎 97.540.11894461 > rawdata[rawdata$group==2,][1:5,] 临床诊断 BIL Alb ALP ALT group 109 肝炎 140.839.02417612 110 肝炎 127.237.012634432 111 急肝 42.830.31876232 112 急肝 112.733.713212322 113 肝炎 292.929.420613642 > rawdata[rawdata$group==3,][1,] 临床诊断 BIL Alb ALP ALT group 124 慢胆 18.345.5711083 > rawdata[rawdata$group==4,][1,] 临床诊断 BIL Alb ALP ALT group 183 急胆 20.946.581344 > rawdata[rawdata$group==5,][1,] 临床诊断 BIL Alb ALP ALT group 217 正常 19.851.9118225
varcomp <- function(covmat,n) { if (is.list(covmat)) { if (length(covmat) < 2) stop("covmat must be a list with at least 2 elements") ps <- as.vector(sapply(covmat,dim)) if (sum(ps[1] == ps) != length(ps)) stop("all covariance matrices must have the same dimension") p <- ps[1] # 样本维度p m <- length(covmat) # 样本组数m if (length(n) == 1) nl <- rep(n,m) elseif (length(n) == m) nl <- n # n存储每组样本的样本数量 else stop("n must be equal length(covmat) or 1") DNAME <- deparse(substitute(covmat)) # data.name } else stop("covmat must be a list") Sl <- covmat # S_l是第l组样本协方差矩阵 # (n_l-1)*S_l S1 <- lapply(1:length(covmat),function(i,mat,n) { (n[i]-1) * mat[[i]] },mat=covmat,n=nl) S <- matrix(colSums(matrix(unlist(S1),ncol=p^2,byrow=T)),ncol=p) S_pooled <- 1/sum(nl-1)*S # S_pooled是合并的样本协方差矩阵 detSl <- sapply(Sl,det) det_S_pooled <- det(S_pooled) # lambda4 <- prod((detSl/det_S_pooled)^((nl-1)/2)) # 这个很容易Nan,直接取对数进行运算 ln_lambda4 <- sum((nl-1)/2*log(detSl/det_S_pooled)) b <- (sum(1/(nl-1)) - 1/sum(nl-1))*(2*p^2+3*p-1)/(6*(p+1)*(m-1)) STATISTIC <- -2*(1-b)*ln_lambda4 mu <- p*(p+1)*(m-1)/2 PVAL <- 1 - pchisq(STATISTIC,mu)
gnum<-length(unique(rawdata$group)) covmat <- vector("list",gnum) for (i in1:gnum){ covmat[[i]]<-cov(data[rawdata$group==i,]) } meanmat <- vector("list",gnum) for (i in1:gnum){ meanmat[[i]]<-apply(data[rawdata$group==i,],2,mean) }