Calculating P-values in R

Notes below are documented for future reference should I ever need to use them again someday.
A pharmaceutical company is interested in testing a potential blood pressure lowering medication. Their first examination considers only subjects that received the medication at baseline then two weeks later. The data are as follows (SBP in mmHg)
SubjectBaselineWeek 2
1140132
2138135
3150151
4148146
5135130
Consider testing the hypothesis that there was a mean reduction in blood pressure? Give the P-value for the associated two sided T test. (Hint, consider that the observations are paired).
Solution:
baseline <- span=""> c(140,138,150,148,135)
week2 <- c(132,135,151,146,130)

round(t.test(baseline,week2, paired=TRUE)$p.value,3)
## [1] 0.087
Researchers conducted a blind taste test of Coke versus Pepsi. Each of four people was asked which of two blinded drinks given in random order that they preferred. The data was such that 3 of the 4 people chose Coke. Assuming that this sample is representative, report a P-value for a test of the hypothesis that Coke is preferred to Pepsi using a one sided exact test.
pbinom(2, size= 4, prob= 0.5, lower.tail=FALSE)
## [1] 0.3125
For more details on the above, refer to Brian Caffo’s video here
Infection rates at a hospital above 1 infection per 100 person days at risk are believed to be too high and are used as a benchmark. A hospital that had previously been above the benchmark recently had 10 infections over the last 1,787 person days at risk. About what is the one sided P-value for the relevant test of whether the hospital is below the standard?
ppois(10, lambda=1/100*1787, lower.tail=TRUE)
## [1] 0.03237153
Suppose that 18 obese subjects were randomized, 9 each, to a new diet pill and a placebo. Subjects’ body mass indices (BMIs) were measured at a baseline and again after having received the treatment or placebo for four weeks. The average difference from follow-up to the baseline (followup - baseline) was -3 kg/m2 for the treated group and 1 kg/m2 for the placebo group. The corresponding standard deviations of the differences was 1.5 kg/m2 for the treatment group and 1.8 kg/m2 for the placebo group. Does the change in BMI appear to differ between the treated and placebo groups? Assuming normality of the underlying data and a common population variance, give a pvalue for a two sided t test.
rnorm2 <-  function(n,mean,sd) { mean+sd*scale(rnorm(n)) }
t.test(rnorm2(9,-3,1.5),rnorm2(9,1,1.8), var.equal=TRUE, alternative = "two.sided")
## 
##  Two Sample t-test
## 
## data:  rnorm2(9, -3, 1.5) and rnorm2(9, 1, 1.8)
## t = -5.1215, df = 16, p-value = 0.0001025
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -5.655699 -2.344301
## sample estimates:
## mean of x mean of y 
##        -3         1
Answer: P value is less than 0.01
Brain volumes for 9 men yielded a 90% confidence interval of 1,077 cc to 1,123 cc. Would you reject in a two sided 5% hypothesis test of H0:ยต=1,078?
No you wouldn’t reject since the stated confidence interval covers only 90%. In a 5% hypothesis test, 95% of the interval would be covered. Surely 1078 which is just 1 more than 1077 - would be covered under that interval.
Researchers would like to conduct a study of 100 healthy adults to detect a four year mean brain volume loss of .01 mm3. Assume that the standard deviation of four year volume loss in this population is .04 mm3. About what would be the power of the study for a 5% one sided test versus a null hypothesis of no volume loss?
n = 100
pnorm(qnorm(0.95) * 0.04/sqrt(n), mean= 0.01, sd=0.04/sqrt(n), lower.tail=FALSE)
## [1] 0.8037649
Researchers would like to conduct a study of n healthy adults to detect a four year mean brain volume loss of .01 mm3. Assume that the standard deviation of four year volume loss in this population is .04 mm3. About what would be the value of n needded for 90% power of type one error rate of 5% one sided test versus a null hypothesis of no volume loss?
Ho:mu=0, Ha:mu>0, sd=0.04, power=80%, mua=0.01
# available info: Ho:mu=0, Ha:mu>0, sd=0.04, power=80%, mua=0.01

# what we need to do:-
# find the sample size needed to get 90% power of type one error rate of 5% one sided test

# Solution: get the null hypothesis's 95% quantiles
# solve this:- 0 + qnorm(0.95) * 0.04/sqrt(n)

# as a start, assume n = 100
n = 100
pnorm(qnorm(0.95) * 0.04/sqrt(n), mean= 0.01, sd=0.04/sqrt(n), lower.tail=FALSE)
## [1] 0.8037649
# we get 80% power. Let's try to increase this.

n = 140
pnorm(qnorm(0.95) * 0.04/sqrt(n), mean= 0.01, sd=0.04/sqrt(n), lower.tail=FALSE)
## [1] 0.9054399

Comments

Popular posts from this blog

HIVE: Both Left and Right Aliases Encountered in Join

A Basic Recipe for Machine Learning

Changing Windows Account password using Outlook