首页 文章

Stata:提取p值并将其保存在列表中

提问于
浏览
0

这可能是一个微不足道的问题,但作为R用户来到Stata我到目前为止未能找到正确的Google术语来找到答案 . 我想执行以下步骤:

  • 做一堆测试(例如lrtest导致foreach循环)

  • 从每个测试中提取p值并将其保存在某种列表中

  • 有一个列表我可以进行进一步的操作(例如,执行多重比较校正)

所以我想知道如何从命令结果中提取p值(或类似的)以及如何将它们保存到我可以使用的类似矢量的对象中 . 这里有一些R代码做了类似的事情:

myData <- data.frame(a=rnorm(10), b=rnorm(10), c=rnorm(10)) ## generate some data
pValue <- c()
for (variableName in c("b", "c")) {
    myModel <- lm(as.formula(paste("a ~", variableName)), data=myData) ## fit model
    pValue <- c(pValue, coef(summary(myModel))[2, "Pr(>|t|)"]) ## extract p-value and save in vector
}
pValue * 2 ## do amazing multiple comparison correction

对我来说,似乎Stata对它的“编程”思维比R少得多 . 如果你有一个可以编程的R用户的任何一般Stata文献推荐,那也将受到赞赏 .

1 回答

  • 2

    这是一种方法,可以将p值保存在矩阵中,然后您可以操纵矩阵,可能使用Stata中的Mata或标准矩阵操作 .

    matrix storeMyP = J(2, 1, .)  //create empty matrix with 2 (as many variables as we are looping over) rows, 1 column
    matrix list storeMyP  //look at the matrix
    
    loc n = 0 //count the iterations
    
    foreach variableName of varlist b c {
    
      loc n = `n' + 1  //each iteration, adjust the count
    
      reg a `variableName'
      test `variableName' //this does an F-test, but for one variable it's equivalent to a t-test (check: -help test- there is lots this can do
      matrix storeMyP[`n', 1] = `r(p)'  //save the p-value in the matrix 
    
    }
    matrix list storeMyP  //look at your p-values 
    matrix storeMyP_2 = 2*storeMyP //replicating your example above
    

    Stata在估计和测试命令之后会自动存储某些数量 . 当帮助文件说这个命令在r()中存储以下值时,用单引号引用它们 . 使用 svmat storeMyP 将矩阵列转换为变量也可能很有趣,或者有关详细信息,请参阅 help svmat .

相关问题