简体   繁体   中英

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

I am going to determine the confidence interval for the proportion based on dataset "CES11" dataset which is included in R. The variable that I have used for the test is Abortion and the values are "No" and "Yes". I want to find the confidence interval for the proportion of "Yes", but R does a hypothesis test and provides a confidence interval for the first level which is "No". How can I change it such that results corresponds to level equal to "Yes"?

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

A quick way in base R is to reverse the tabulation vector order:

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 

Otherwise, you can likely just relevel the factor:

df_ces11 <- carData::CES11
df_ces11$abortion <- factor(df_ces11$abortion, levels=c("Yes", "No"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM