1

我将根据 R 中包含的数据集“CES11”数据集确定比例的置信区间。我用于测试的变量是 Abortion,值是“No”和“Yes”。我想找到“是”比例的置信区间,但 R 做了一个假设检验,并为第一级“否”提供了一个置信区间。如何更改它以使结果对应于等于“是”的级别?

Frequency counts (test is for first level):
abortion
 No  Yes 
1818  413 

   1-sample proportions test without continuity correction

data:  rbind(.Table), null probability 0.5
X-squared = 884.82, df = 1, p-value < 2.2e-16
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
0.7982282 0.8304517
sample estimates:
       p 
0.8148812
4

1 回答 1

1

基数 R 中的一种快速方法是反转制表向量顺序:

cts_abortion <- table(carData::CES11$abortion)

## p is for rate of "No"
prop.test(cts_abortion)
##  1-sample proportions test with continuity correction
## 
## data:  cts_abortion, null probability 0.5
## X-squared = 883.56, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.7979970 0.8306679
## sample estimates:
##        p 
## 0.8148812 

## p is for rate of "Yes"
prop.test(rev(cts_abortion))
##  1-sample proportions test with continuity correction
## 
## data:  rev(cts_abortion), null probability 0.5
## X-squared = 883.56, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1693321 0.2020030
## sample estimates:
##         p 
## 0.1851188 

否则,您可能只需重新调整因子:

df_ces11 <- carData::CES11
df_ces11$abortion <- factor(df_ces11$abortion, levels=c("Yes", "No"))
于 2022-02-12T20:53:13.723 回答