在R commander中查找分类变量第二级的置信区间

Finding confidence interval for the second level of a categorical variable in R commander

我将根据 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

以 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"))