Remember that you can get more detailed information about a command by typing it in the Console with a question mark in front. For example, ?prop.test.

In general, each command produces a list of objects that relate to the output for future access. You can save the list by giving it a name. See the first examples for prop.test below.

1 Inference for Propotion

prop.test(x, n, p = NULL, alternative = c(“two.sided”, “less”, “greater”), conf.level = 0.95, data = NULL, success = NULL, …)

In the output, X-squared is the square of the z-score, which is stored as statistic in the output list.

1.1 Summarized data

1.1.1 One-sample example

prop.test(14,50, correct = FALSE)
## 
##  1-sample proportions test without continuity correction
## 
## data:  14 out of 50
## X-squared = 9.68, df = 1, p-value = 0.001863
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1747417 0.4166512
## sample estimates:
##    p 
## 0.28
## Default value of the null hypothesis is p=0.5.
## Two-sided test is the default.
## Just ignore the result of hypothesis test if you are looking for CI.
## correct = FALSE to match the formula you learned in class.

Save the result of the test:

result = prop.test(14,50, correct = FALSE)

You can see what values are store in result:

summary(result)
##             Length Class  Mode     
## statistic   1      -none- numeric  
## parameter   1      -none- numeric  
## p.value     1      -none- numeric  
## estimate    1      -none- numeric  
## null.value  1      -none- numeric  
## conf.int    2      -none- numeric  
## alternative 1      -none- character
## method      1      -none- character
## data.name   1      -none- character

Access a stored value by using $ after object name, followed by the name of the value:

result$statistic
## X-squared 
##      9.68

To get the absolute value of z-score:

z=sqrt(result$statistic); names(z)="|Z-score|"; z  
## |Z-score| 
##   3.11127
## You have to choose the sign based on wehter your p-hat is above or below the null value. In this example, since p-hat=0.28 < 0.5=null value, the z-score is really negative.
prop.test(14,50,0.3, alternative="less", correct = FALSE)
## 
##  1-sample proportions test without continuity correction
## 
## data:  14 out of 50
## X-squared = 0.095238, df = 1, p-value = 0.3788
## alternative hypothesis: true p is less than 0.3
## 95 percent confidence interval:
##  0.0000000 0.3936471
## sample estimates:
##    p 
## 0.28

1.2 Raw data

1.2.1 One Sample

The code book for the data loaded below can be found HERE.

The data is loaded directly from an URL into the data frame df. df$Sex refers to the variable sex. In the code book, males are coded as “0”. The argument success is used to identified which the sample poportion is studied. Thus, the example below is about proportion of males in the dataset.

df = read.csv(url("http://www.lock5stat.com/datasets/ICUAdmissions.csv"))
prop.test(df$Sex, success = "0") ## For some reason, the argument "correction = FALSE" doesn't work when raw data is used.
## 
##  1-sample proportions test with continuity correction
## 
## data:  df$Sex  [with success = 0]
## X-squared = 11.045, df = 1, p-value = 0.0008893
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.5485320 0.6867787
## sample estimates:
##    p 
## 0.62

1.2.2 Two Sample

df2 = df$Sex[df$Sex==0]

2 Inference for Mean

2.1 Summarized data

tsum.test(mean.x, s.x = NULL, n.x = NULL, mean.y = NULL, s.y = NULL, n.y = NULL, alternative = “two.sided”, mu = 0, var.equal = FALSE, conf.level = 0.95)

2.1.1 One Sample