簡體   English   中英

r 中 binom.test() 的循環

[英]loop for binom.test() in r

我有一個我正在運行二項式測試的成功、概率和樣本大小的數據集。

這是數據樣本(請注意,實際數據集讓我運行了 >100 個二項式測試):

km      n_1 prey_pred p0_prey_pred
 <fct> <dbl>     <int>        <dbl>
 80       93        12       0.119 
 81     1541       103       0.0793
 83      316         5       0.0364
 84      721        44       0.0796
 89      866        58       0.131 

我通常運行這個(第一行的例子):

n=93
p0=0.119
successes=12

binom.test(obs.successes, n, p0, "two.sided") 

>   Exact binomial test

data:  12 and 93
number of successes = 12, number of trials = 93, p-value = 0.74822
alternative hypothesis: true probability of success is not equal to 0.119
95 percent confidence interval:
 0.068487201 0.214548325
sample estimates:
probability of success 
            0.12903226 

有沒有辦法系統地讓它對每一行數據運行多個二項式檢驗,然后將所有 output(p 值、置信區間、成功概率)存儲為單獨的列?

我已經嘗試過這里提出的解決方案,但我顯然是 m

您可以按照評論中的建議為此定義 function :

my_binom <- function(x, n, p){
res <- binom.test(x, n, p)
out <- data.frame(res$p.value, res$conf.int[1], res$conf.int[2], res$estimate)
names(out) <- c("p", "lower_ci", "upper_ci", "p_success")
rownames(out) <- NULL
return(out)
}

然后你可以為每一行應用它

do.call("rbind.data.frame", apply(df, 1, function(row_i){
my_binom(x= row_i["prey_pred"], n= row_i["n_1"], p= 
row_i["p0_prey_pred"])
}))

使用apply

res <- t(`colnames<-`(apply(dat, 1, FUN=function(x) {
  rr <- binom.test(x[3], x[2], x[4], "two.sided")
  with(rr, c(x, "2.5%"=conf.int[1], estimate=unname(estimate), 
             "97.5%"=conf.int[2], p.value=unname(p.value)))
}), dat$km))
res
#    km  n_1 prey_pred p0_prey_pred        2.5%   estimate      97.5%      p.value
# 80 80   93        12       0.1190 0.068487201 0.12903226 0.21454832 7.482160e-01
# 81 81 1541       103       0.0793 0.054881013 0.06683971 0.08047927 7.307921e-02
# 83 83  316         5       0.0364 0.005157062 0.01582278 0.03653685 4.960168e-02
# 84 84  721        44       0.0796 0.044688325 0.06102635 0.08106220 7.311463e-02
# 89 89  866        58       0.1310 0.051245893 0.06697460 0.08572304 1.656621e-09

編輯

如果您有多個列集,采用寬格式(並且出於某種原因想要留在那里)

dat2 <- `colnames<-`(cbind(dat, dat[-1]), c("km", "n_1.1", "prey_pred.1", "p0_prey_pred.1", 
                                            "n_1.2", "prey_pred.2", "p0_prey_pred.2"))

dat2[1:3,]
#   km n_1.1 prey_pred.1 p0_prey_pred.1 n_1.2 prey_pred.2 p0_prey_pred.2
# 1 80    93          12         0.1190    93          12         0.1190
# 2 81  1541         103         0.0793  1541         103         0.0793
# 3 83   316           5         0.0364   316           5         0.0364

你可以這樣做:

res2 <- t(`colnames<-`(apply(dat2, 1, FUN=function(x) {
  rr1 <- binom.test(x[3], x[2], x[4], "two.sided")
  rr2 <- binom.test(x[6], x[5], x[7], "two.sided")
  rrr1 <- with(rr1, c("2.5%.1"=conf.int[1], estimate.1=unname(estimate), 
                      "97.5%.1"=conf.int[2], p.value.1=unname(p.value)))
  rrr2 <- with(rr2, c("2.5%.1"=conf.int[1], estimate.1=unname(estimate), 
                      "97.5%.1"=conf.int[2], p.value.1=unname(p.value)))
  c(x, rrr1, rrr2)
}), dat2$km))
res2
#    km n_1.1 prey_pred.1 p0_prey_pred.1 n_1.2 prey_pred.2 p0_prey_pred.2      2.5%.1
# 80 80    93          12         0.1190    93          12         0.1190 0.068487201
# 81 81  1541         103         0.0793  1541         103         0.0793 0.054881013
# 83 83   316           5         0.0364   316           5         0.0364 0.005157062
# 84 84   721          44         0.0796   721          44         0.0796 0.044688325
# 89 89   866          58         0.1310   866          58         0.1310 0.051245893
#    estimate.1    97.5%.1    p.value.1      2.5%.1 estimate.1    97.5%.1    p.value.1
# 80 0.12903226 0.21454832 7.482160e-01 0.068487201 0.12903226 0.21454832 7.482160e-01
# 81 0.06683971 0.08047927 7.307921e-02 0.054881013 0.06683971 0.08047927 7.307921e-02
# 83 0.01582278 0.03653685 4.960168e-02 0.005157062 0.01582278 0.03653685 4.960168e-02
# 84 0.06102635 0.08106220 7.311463e-02 0.044688325 0.06102635 0.08106220 7.311463e-02
# 89 0.06697460 0.08572304 1.656621e-09 0.051245893 0.06697460 0.08572304 1.656621e-09

可以將其編碼得更嵌套,但我建議讓事情變得簡單,以便以后其他人更好地理解發生了什么,並且可能包括自己。


數據:

dat <- read.table(text="km      n_1 prey_pred p0_prey_pred
 80       93        12       0.119 
 81     1541       103       0.0793
 83      316         5       0.0364
 84      721        44       0.0796
 89      866        58       0.131 ", header=TRUE)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM