Setup

options(width = 40)
library(tidyverse); library(plotly)
## ── Attaching core tidyverse packages ───
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'plotly'
## 
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## 
## The following object is masked from 'package:graphics':
## 
##     layout
knitr::opts_chunk$set(echo=FALSE, message=FALSE, warning=FALSE, comment=NA,
                      fig.width=7, fig.height=3.6, out.width='100%',
                      fig.align='center', dev='png', dpi=96)
options(width=60)
set.seed(42)
n <- 25; nsim <- 10000
xbar <- replicate(nsim, mean(rnorm(n)))
z <- sqrt(n)*xbar
sim <- tibble(z=z,xbar=z/sqrt(n))
sdx <- 1/sqrt(n)

xbar under H0

test statistic

\[ Z=\frac{\bar X-0}{1/\sqrt{n}}=\sqrt{n}\,\bar X\sim N(0,1)\ (H_0). \]

p-value

\[ p= P(|Z|\ge|z_{obs}|)=2\big(1-\Phi(|z_{obs}|)\big). \]

formula p-value

z_obs<-2.1; 2*(1-pnorm(abs(z_obs)))
[1] 0.03572884

Visual

Plotly

Example (one-sample Z, two-sided)

H0: mu=0; n=25; xbar=0.42; z=2.10; p=0.0357 -> REJECT H0 at alpha=0.05
Simulation check: p≈0.0382 (nsim=10000)