【问题标题】:Simulated dataset for boot strapping analysis用于引导分析的模拟数据集
【发布时间】:2012-03-15 10:21:04
【问题描述】:

我的目标是使用自举(1000 次重复)来计算 r(皮尔逊相关系数)的空分布、均值和 CI,这些与从我的 600 个唯一个体(ID )。我最近从 SAS 切换到 R,我将使用“procsurveyselect”来生成数据集。问题:

  1. 生成这些结果的最有效方法是什么(请参阅下面的尝试)?
  2. 在我的示例中,我将如何使用 set.seed 命令来复制我的结果?

具有 600 个人和相关特征值的模拟起始数据集:

ID <- seq(1, 600, by = 1)
x <- rnorm(600, m = 7, sd = 2)
X <- as.data.frame(cbind(ID, x))

然后我生成 r 的 1000 次重复并计算 95% CI:

for (i in 1:1000) { 
  X.sample <- X[ sample(1:nrow(X), 40, replace = FALSE), ] 
  X.sample.1 <- X.sample[1:20, ]
  X.sample.2 <- X.sample[21:40, ]
  Y <- as.data.frame(cbind(X.sample.1$ID, X.sample.1$x, X.sample.2$ID,  X.sample.2$x))
  cor.results <- cor.test(Y[,2], Y[,4], alternative = c("greater"), method = c("pearson"))
  Z[i] <- cor.results$estimate
}

error <- qt(0.975, df = (length(Z) - 1)) * (sd(Z))/sqrt(length(Z))

【问题讨论】:

  • 只需几个 cmets 重新编码简洁... ID 列在这里似乎无关紧要,但如果你确实想要它,ID &lt;- 1:600 会做的伎俩。在这种情况下,我看不出有任何理由使用data.frame,因为您的IDx 是相同的数据类型(数字)。据我所知,matrix 操作通常比data.frame 操作快。请参阅下面的解决方案以了解其他一些节省时间的方法。

标签: r random-sample statistics-bootstrap


【解决方案1】:

试穿一下尺寸:

# generate dataset
set.seed(1)
X <- rnorm(600, 7, 2)

# Create a function that samples 40 elements from X,
#  and calculates Pearson's r for the first 20 elements 
#  against the last 20 elements.
booties <- function(x) {
  X.samp <- sample(x, 40)
  cor(X.samp[1:20], X.samp[21:40])
}

# Replicate this function 1000 times (spits out a vector of cor estimates)
Z <- replicate(1000, booties(X))
error <- qt(0.975, length(Z)-1 * sd(Z)/sqrt(length(Z)))

完成 1000 次重复大约需要 0.08 秒(比您正在试验的 for 循环快大约一个数量级)。

【讨论】:

    【解决方案2】:

    一般来说,隐式循环比显式循环更快。尝试将代码放入循环中并将其放入函数中,然后在 lapply 或 sapply 语句中使用该函数。

    myfunction = function(<insert relevant parameters here>)
    { 
      X.sample <- X[ sample(1:nrow(X), 40, replace = FALSE), ] 
      X.sample.1 <- X.sample[1:20, ]
      X.sample.2 <- X.sample[21:40, ]
      Y <- as.data.frame(cbind(X.sample.1$ID, X.sample.1$x, X.sample.2$ID,  X.sample.2$x))
      cor.results <- cor.test(Y[,2], Y[,4], alternative = c("greater"), method = c("pearson"))
      cor.results$estimate
    }
    
    Z  = sapply(x, myfunction)
    #Here every element of x contains the arguments you want to pass to my function
    #You can pass multiple arguments separated by commas after the function name
    
    error <- qt(0.975, df = (length(Z) - 1)) * (sd(Z))/sqrt(length(Z))
    

    你可以这样做,但我发现如果可以的话,最好只使用 boot 包中的 boot() 函数。

    至于set.seed(),您需要在每次生成随机任何东西之前直接设置它。见下文。

    > rnorm(6)
    [1]  1.0915017 -0.6229437 -0.9074604 -1.5937133  0.3026445  1.6343924
    > set.seed(1001)
    > rnorm(6)
    [1]  2.1886481 -0.1775473 -0.1852753 -2.5065362 -0.5573113 -0.1435595
    > set.seed(1001)
    > rnorm(6)
    [1]  2.1886481 -0.1775473 -0.1852753 -2.5065362 -0.5573113 -0.1435595
    > rnorm(6)
    [1]  1.0915017 -0.6229437 -0.9074604 -1.5937133  0.3026445  1.6343924
    
    
    > set.seed(1001)
    > sample(1:5,10,replace=T)
     [1] 5 3 3 3 3 5 1 1 2 4
    > sample(1:5,10,replace=T)
     [1] 3 1 5 3 2 5 1 2 1 4
    > set.seed(1001)
    > sample(1:5,10,replace=T)
     [1] 5 3 3 3 3 5 1 1 2 4
    > rnorm(6)
    [1] -0.1435595  1.0915017 -0.6229437 -0.9074604 -1.5937133  0.3026445
    > set.seed(1001)
    > rnorm(6)
    [1]  2.1886481 -0.1775473 -0.1852753 -2.5065362 -0.5573113 -0.1435595
    

    希望有帮助!

    在研究 boot 函数以提供示例时,我遇到了障碍。它只返回一行。奇怪的!我可能会对此提出一个新问题。无论如何,我认为bootstrap 包中的bootstrap() 函数可以满足您的需求。 这是我的例子

    set.seed(1001)
    X <- rnorm(600, 7, 2)
    
    
    myStat <- function(x, pairs) {
    index = sample(1:length(x),(pairs*2))
    Z = cor(X[index[1:(length(index)/2)]], X[index[((length(index)/2)+1):length(index)]])
    return(Z)
    }
    
    b=bootstrap(X,1000,myStat,pairs=20)
    Z <- b$thetastar
    error <- qt(0.975, length(Z)-1 * sd(Z)/sqrt(length(Z)))
    

    【讨论】:

    • 感谢您的好建议。您能否就如何创建数据集提出建议(注意 ID 字段上方的好建议是完全没有必要的)并使用 boot 命令?我试过了,这就是为什么我感觉回到了不太完美的 for 循环。谢谢,基思
    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 2019-03-04
    • 2014-08-23
    • 2014-08-27
    • 1970-01-01
    • 2021-07-01
    • 2013-07-18
    • 2014-03-27
    相关资源
    最近更新 更多