Sample size calculation: z or t I am confused about what I've seen in my textbook to calculate the sample size for a specified confidence interval width (for a mean).
The book says whether you know $\sigma$ or have $S$ from a previous study, you can use
$$z_{\alpha /2}\frac{\sigma}{\sqrt{n}}=x$$
where $x$ is half the width of the interval.
What doesn't make sense to me is that if you use that formula using a value $S$ instead of $\sigma$, shouldn't you be using a $t$ value instead of $z$? Which requires $n$ itself, but maybe you could use an iterative method where you choose an initial $n$ and keep updating the value of $t$ until convergence.
The reason I think $t$ should be used is because when you actually go and collect your data, you will be constructing the interval with $\bar{Y} \pm t_{\alpha/2,n-1}\frac{S}{\sqrt{n}}$, so if you used $z$ to calculate the sample you probably won't end up with the correct width, will you?

On top of @dipetkov's great and detailed answer, a (not so) small warning : these confidence intervals are accurate and true only for Normally Distributed (independent) random variables, and approximately true for n sufficiently large (but how approximate and how large?).
if $\forall i,  X_i \sim  \mathcal{N}(\mu, \sigma^2)$ and independent then we have $$\frac{1}{n} \sum_i^n X_i = \overline{X_n} \sim \mathcal{N}(\mu, \frac{\sigma^2}{n})$$
So indead if we know in advance the std $\sigma$, a $1-\alpha$ CI of the mean estimation $\hat{\mu}$ is $\overline{x_n} \pm z_{\alpha/2} \frac{\sigma}{\sqrt{n}}$. no limits here, and if you run simulations, fix an $n$, the average nb of experiments that ends with $\mu \in CI(x_1, ... x_n)$ will tend to exactly $1-\alpha$ when the nb of experiments tend to $\infty$. math here, no approximations.
When you don't know in advance the std $\sigma$ of your normal distribution, you use the Student distribution, which is, by definition, a random variable $\mathcal{T}_m$ ($m$ number of degrees of freedom) that has same law as a $ Z / \sqrt{U/m}$ when $Z \sim \mathcal{N}(0,1)$ and $U \sim \chi^2_m$. Math tells us that $$\sqrt{n}\Big(\frac{\overline{X_n} - \mu}{S_n}\Big) \sim \mathcal{T}_{n-1}$$
Hence the $1-\alpha$ CI for the estimator of $\mu$ : $\overline{x_n} \pm t_{\alpha/2,n-1} \frac{s_n}{\sqrt{n}}$. Again, this is true for all n, not just approximately true when n is large or anything
Now the warning : when $X$ does not have a Normal Distribution, these CI on the mean estimation are wrong : the probability of the real $\mathbb{E}[X]$ (if it exists! see Cauchy distributions for an example without it) being inside the CI computed on a particular experiment of n samples is not $1-\alpha$. Yes, the TCL says that when n tends to $\infty$, the r.v. $\sqrt{n}\frac{\overline{X_n} - \mu}{\sigma}$ converges in distribution to $ \mathcal{N}(0, 1)$ but we don't actually know starting from what $n$ this approximation will be "almost true" (or at least negligeable to have almost $1-\alpha$ experiments ending with $\mu \in CI$. One usually says $n > 30$ as a rule of thumb, but be careful : try experiments for n = 100, $ X \sim \mathcal{B}(p)$ a Bernoulli distribution with variable $p$ between 0 and 1.
see this article for detailed work, fun figure n°3. Do reproduce it at home.
