# KeapNotes blog

Stay hungry, Stay Foolish.

0%

#### Calculating p Values

``````z <- (x - m)/(s / sqrt(n))
2 * pnorm(-abs(z))
# another way when z > 0
2 * (1 - pnorm(z))``````

``2 * (1 - pnorm(x, mean = m, sd = s / sqrt(n)))``

``````t <- (x - m)/(s / sqrt(n))
2 * pt(-abs(t), df = n-1)``````

#### Calculating Confidence Intervals

``````error <- qnorm(0.975) * (s / sqrt(n))
c(m - error, m + error)``````

``````error <- qt(0.975, df = n-1) * (s / sqrt(n))
c(m - error, m + error)``````

#### Calculating The Power Of A Test

原假设为真 备择假设为真

``````error <- qnorm(0.975) * (s / sqrt(n))
assumed <- m + 1.5
Zleft <- ((m - error) - assumed) / (s / sqrt(n))
Zright <-((m + error) - assumed) / (s / sqrt(n))
p <- pnorm(Zright) - pnorm(Zleft)
1 - p``````

``````error <- qt(0.975, df = n-1) * (s / sqrt(n))
assumed <- m + 1.5
tleft <- ((m - error) - assumed) / (s / sqrt(n))
tright <-((m + error) - assumed) / (s / sqrt(n))
p <- pt(tright, df=n-1) - pt(tleft, df=n-1)
1 - p

'The idea is that you give it the critical t scores and
the amount that the mean would be shifted if the alternate
mean were the true mean'
1 - pt(t,df=n-1, ncp=ncp) - pt(-t,df=n-1, ncp=ncp)``````