如何在r中多次运行该函数

时间:2016-04-04 20:29:59

标签: r

我希望运行Nobel函数10次,得到一个Percent_lnOR和Percent_Rho值的表,这样每次迭代我都会得到这两个值。

如何多次运行该函数(比方说10次),并获得Percent_lnOR和Percent_Rho的值表?

library(gtools)

Nobel<-function(n){
  p11hat<-rep(0,n)
  p12hat<-rep(0,n)
  p21hat<-rep(0,n)
  p22hat<-rep(0,n)
  Rho<- rep(0,n)
  AsyVarRho<-rep(0,n)
  OddsRatio<-rep(0,n)
  lnOR<-rep(0,n)
  AsyVarOR<-rep(0,n)
  count_lnOR<-rep(0,n)
  count_Rho<-rep(0,n)


  for (i in 1:n) { 
    MB <- rdirichlet(1, c(1,1,1,1))
    #print (MB)
    Sample<-rmultinom(n, 100, prob= c(MB[1,1], MB[1,2], MB[1,3], MB[1,4]))

    p11hat <- Sample[1,i]/100
    p12hat <- Sample[2,i]/100
    p21hat <- Sample[3,i]/100
    p22hat <- Sample[4,i]/100


    p1p <- p11hat + p12hat
    p2p <- 1-p11hat-p12hat
    pp1 <- p11hat+p21hat
    pp2 <- 1-p11hat-p21hat
    Var1 <- p11hat*(1-p11hat)
    Var2 <- p12hat*(1-p12hat)
    Var3 <- p21hat*(1-p21hat)
    Cov12 <- -p11hat*p12hat
    Cov13 <- -p11hat*p21hat
    Cov23 <- -p12hat*p21hat
    U <- sqrt(p1p*p2p*pp1*pp2)
    V <- p11hat*p2p*pp2 - p12hat*p2p*pp1 - p21hat*p1p*pp2 + (1-p11hat-p12hat-p21hat)*p1p*pp1

    DUDx <- (1-p1p-pp1)*(p1p+pp1-2*p1p*pp1)
    DUDy <- pp1*(1-pp1)*(1-2*p1p)
    DUDz <- p1p*(1-p1p)*(1-2*pp1)
    DVDx <- 1-p1p-pp1
    DVDy <- -pp1
    DVDz <- -p1p
    DfDx <- (1/sqrt(U))*DVDx - 0.5*U^(-1.5)*V*DUDx
    DfDy <- (1/sqrt(U))*DVDy - 0.5*U^(-1.5)*V*DUDy
    DfDz <- (1/sqrt(U))*DVDz - 0.5*U^(-1.5)*V*DUDz
    AsyVarRho <- Var1*DfDx^2 + Var2*DfDy^2 + Var3*DfDz^2 + 2*Cov12*DfDx*DfDy +
      2*Cov13*DfDx*DfDz + 2*Cov23*DfDy*DfDz

    OddsRatio <- p11hat*(1-p11hat-p12hat-p21hat)/(p12hat*p21hat)
    lnOR <- log(OddsRatio)

    AsyVarOR <- (1/p11hat + 1/p12hat + 1/p21hat + 1/(1 - p11hat - p12hat - p21hat))
    Rho <- V/sqrt(U)
    z1<-lnOR/sqrt(AsyVarOR/100)
    z2<- Rho/ sqrt(AsyVarRho/100)
    count_lnOR<-ifelse(abs(z1)>1.96,1,0)
    count_Rho<-ifelse(abs(z2)>1.96,1,0)

  }

  df <- data.frame(p11hat, p12hat, p21hat, Rho, AsyVarRho, OddsRatio,lnOR, AsyVarOR,count_lnOR,count_Rho )
  return(df)
}

Y<-Nobel(10)
Y

C_lnOR<-as.matrix(table( Y$count_lnOR))
C_Rho<-as.matrix(table( Y$count_Rho))
Percent_lnOR<- (C_lnOR[2]/ sum(C_lnOR[1], C_lnOR[2]))*100 
Percent_Rho<- (C_Rho[2]/ sum(C_Rho[1], C_Rho[2]))*100 

2 个答案:

答案 0 :(得分:2)

这个怎么样:

replicate(n = 10, expr = {
  Y <- Nobel(10)
  C_lnOR <- as.matrix(table(Y$count_lnOR))
  C_Rho <- as.matrix(table(Y$count_Rho))
  Percent_lnOR <- (C_lnOR[2] / sum(C_lnOR[1], C_lnOR[2])) * 100 
  Percent_Rho <- (C_Rho[2] / sum(C_Rho[1], C_Rho[2])) * 100 
  c(pct_lnOR = Percent_lnOR, pct_rho = Percent_Rho)
})

它应该给你一个很好的矩阵,其中rownames是“pct_lnOR”和“pct_rho”,每列都是一个复制。

答案 1 :(得分:0)

你的功能很好,你只需要rbind(读取row-bind)迭代调用的结果。例如,在最简单的例子中:

Y <- data.frame()
myvector <- c(1:10)  #put your values here
for i in 1:10
{
  Y <- rbind(Y, Nobel(myvector[i]))
}