Espartaco

“Is that to say we are against Free Trade? No, we are for Free Trade, because by Free Trade all economical laws, with their most astounding contradictions, will act upon a larger scale, upon the territory of the whole earth; and because from the uniting of all these contradictions in a single group, where they will stand face to face, will result the struggle which will itself eventuate in the emancipation of the proletariat.”

Karl Heinrich Marx · Marx-Engels Collected Works, Vol. VI, p. 290

28,796 views since December 2020

28,796 visitas desde diciembre de 2020

EnglishEspañol

GENERAL PRINCIPLES OF THE ESTIMATION AND HYPOTHESIS TESTING PROCESS IN R STUDIO. PART II, ​​R STUDIO CODE WITH COMMENTS

Mathematical statistics · Methods notebook

Part II · Revised and expanded edition

Foundations of Estimation and Hypothesis Testing in R

Theory, simulation, annotated code, and interpretation: from the distribution of the data to the design of a study.

JOSÉ MAURICIO GÓMEZ JULIÁN

Originally published September 6, 2021
Comprehensive revision: October 29, 2026

A hypothesis test does not begin with a function call. It begins by specifying the phenomenon under investigation, how the data were obtained, and which statement about the population can be examined using those data. R performs the calculations; understanding their foundations makes it possible to determine when those calculations address the research question and which conclusions they support.

This notebook connects descriptive statistics, simulation, estimation, and inference through a sequence of examples: a sample of stores, computationally generated normal populations, comparisons of means and variances, paired sleep measurements, and power calculations. Each procedure is developed through its question, mathematical foundations, implementation, and interpretation. R is the language and computing environment; RStudio is an integrated development environment that can be used to work with it.

01 / Foundations

From a scientific question to a statistical decision

The population is the set of units or the process to which the investigation refers; the sample is the collection of available observations. A parameter, such as the population mean μ or population variance σ², characterizes the population. An estimator, such as the sample mean X̄ or sample variance S², is a function of the sample. Its value calculated from the observed data is an estimate.

The distinction is essential: the data are observed; the parameter is usually unknown; the estimator varies from sample to sample. Inference studies that variation under a model and a data-collection design. A large sample does not, by itself, correct selection bias or make dependent observations independent.

X̄ = (1/n) ∑ᵢ₌₁ⁿ XᵢS² = [1/(n − 1)] ∑ᵢ₌₁ⁿ (Xᵢ − X̄)²Estimated SE of X̄ = S / √n

The standard deviation summarizes the dispersion of the observations. The standard error describes the dispersion of an estimator across samples. For independent, identically distributed observations with finite variance, the standard error of the mean is σ/√n; when σ is unknown, it is estimated by S/√n. This expression requires adjustment when there is dependence, clustering, weighting, or a complex sampling design.

Hypotheses, the test statistic, and the p-value

A null hypothesis H₀ specifies the reference model. For a mean, it may be stated as H₀: μ = μ₀. The two-sided alternative is H₁: μ ≠ μ₀; a one-sided alternative specifies μ > μ₀ or μ < μ₀. The direction must follow the scientific question and be established before using the observed result to choose it.

The test statistic summarizes the discrepancy between the data and H₀ on a scale whose distribution can be studied. The p-value is the probability, under H₀ and the model’s other assumptions, of obtaining a statistic at least as extreme as the observed one, as defined by the chosen alternative. It is not the probability that H₀ is true or the probability that the result is due to chance. Statistical significance does not measure the size or importance of an effect.[1]

Four possible outcomes of a testing rule
DecisionH₀ is trueA specific alternative is true
Reject H₀Type I error; probability α for an exact test of size α.Detection; probability 1 − β.
Do not reject H₀A decision compatible with H₀.Type II error; probability β for that effect.

The level α sets an upper bound on the testing rule’s Type I error; for conservative procedures, the actual probability may be lower. β depends on the true effect, sample size, variability, and test. Failing to reject H₀ may reflect compatibility with it, limited precision, or insufficient ability to detect a relevant effect. It does not establish equality.

A statistical result acquires meaning when it is related back to the phenomenon, the unit of measurement, and the conditions under which the data were produced.

The guiding principle of this notebook

02 / Preparation

Data, importing, and structure

The original example considers a random sample of 21 stores from different parts of the country. It records a cleanliness indicator, an index of products repaired and marketed, and units sold during the previous month. This edition retains that scenario as an exercise; the values below are illustrative. They do not support inferences about an actual population.

Variable dictionary for the exercise
VariableMeaningCaution
menor16A cleanliness indicator; higher values represent greater cleanliness.The original name is retained. The original scale must be known to decide whether its intervals are comparable.
ipcThe percentage of defective products that were repaired and subsequently marketed.This is not a consumer price index. The operational definition of the denominator must be checked.
ventasThe quantity sold during the previous month.This is a count; a continuous approximation requires attention to its distribution and the purpose of the analysis.

Within an RStudio project, it is useful to work with paths relative to the project directory. getwd() reports the current working directory; setwd("actual/path") changes it. The original string "(...)" is a placeholder, not an executable path. The following importing option assumes that the file has no header and contains three columns separated by spaces or tabs:

getwd()
# setwd("actual/path/to/project")  # only if you need to change it
# If you have the original file, without a header:
# estudios <- read.table("estudios.txt", header = FALSE,
#                        col.names = c("menor16", "ipc", "ventas"))

read.table() reads the file; col.names assigns names to the columns of the data frame. It does not create the physical file. If the file already contains column names, use header = TRUE; if it uses a different separator or decimal mark, adjust sep and dec. Reading it twice, first without assignment and then with assignment, is unnecessary.

To run the article without that file, use the following complete dataset:

estudios <- data.frame(
    menor16 = c(12,15,11,18,14,16,13,19,17,12,20,15,18,14,16,19,13,17,11,20,18),
    ipc = c(11,12,14,16,13,15,10,16,14,12,18,20,17,22,19,24,18,21,23,17,20),
    ventas = c(145,158,172,183,167,192,151,176,164,180,188,205,194,217,201,229,182,212,221,196,208)
  )

The commands names(), nrow(), ncol(), dim(), and str() check names, row count, column count, dimensions, and structure. Here they should identify 21 rows and three numeric columns. Original variable and object identifiers are retained in the English edition to preserve correspondence with the Spanish code; comments, explanations, and graphic labels are translated.

stopifnot(all(c("menor16", "ipc", "ventas") %in% names(estudios)),
          all(vapply(estudios, is.numeric, logical(1))))
print(names(estudios)); print(nrow(estudios)); print(ncol(estudios))
print(dim(estudios)); str(estudios)
print(colSums(is.na(estudios)))
if (any(!is.finite(as.matrix(estudios)))) {
  stop("There are missing or non-finite values: document their treatment before analysis.")
}
stopifnot(nrow(estudios) >= 2L)

Explicit access and the historical use of attach()

attach(estudios) adds the variables to the search path, allowing their names to be written without the data-frame prefix; detach(estudios) removes that entry. Understanding these commands is useful because they appear in the original, but an identically named object may mask another, and subsequent changes to the data frame are not necessarily reflected as expected. The revised code uses estudios$ventas, with(estudios, ...), or the argument data = estudios. The assignment ventas <- estudios$ventas creates an explicit vector and must be updated if the data frame is subsequently modified.

03 / Exploration

Means, dispersion, quantiles, and histograms

Description precedes testing. The mean and standard deviation convey different aspects of the data; quantiles and graphical displays can reveal asymmetry, extreme observations, or clusters that a single summary may conceal.

ventas <- estudios$ventas
mean(ventas)
sd(ventas)
var(ventas)
apply(estudios, 2, mean)
apply(estudios, 2, sd)
quantile(ventas)
apply(estudios, 2, quantile)
qv <- quantile(ventas, probs = c(0.025, 0.975))
qv

apply(estudios, 2, mean) applies the function to each column; MARGIN = 1 would apply it by row. There is no need to execute mean() or quantile() beforehand. With a mixed-type data frame, apply() may convert everything into a character matrix; this is why the columns are checked to be numeric here. vapply() and lapply() can work column by column without that global conversion.[2]

What quantiles express

Quantiles locate values of the variable from cumulative proportions. By default, quantile(ventas) returns the minimum, quartiles, and maximum; probs = c(0.025, 0.975) requests the 2.5th and 97.5th percentiles. R provides several algorithms; the default, type = 7, uses interpolation.[3]

In the illustrative records, the mean is 187.667, the standard deviation is 23.258, and these two percentiles are 148 and 225. The values 141 and 243 mentioned in the original are not reproduced here because they belong to a different data file. With small samples, tail quantiles are especially sensitive to a few observations.

tinta <- "#2c2418"; cobre <- "#8b4513"; beige <- "#d4c8b0"
par(col = tinta, col.axis = tinta, col.lab = tinta, col.main = tinta,
    bg = "#fffdf7", las = 1)
hist(ventas, breaks = 7, col = beige, border = cobre,
     xlab = "Sales", ylab = "Frequency", main = "Sales: illustrative data")
abline(v = qv, col = cobre, lty = 2)

abline(v = qv) draws vertical lines at the quantiles; col changes their color and lty their line type. A numeric breaks value suggests a number of classes, but R may choose convenient boundaries and return a different number. To require precise boundaries, provide an explicit vector of breakpoints. When classes have different widths, distinguishing frequencies from densities is important.

Sales distribution for 21 illustrative stores, with lines at percentiles 148 and 225.
Figure 01 Dashed lines: the 2.5th and 97.5th percentiles (148 and 225). These describe the data; they are not a confidence interval for their mean.

04 / Simulation

Normal populations and user-defined functions

We adopt the notation X ∼ N(μ, σ²), with variance as the second parameter. In R, however, rnorm(n, mean, sd) takes the standard deviation. Thus, rnorm(1000, 35, 2) generates 1,000 observations from N(35, 4). In this call, 35 and 2 are parameters of the generating distribution, not the mean and standard deviation actually observed in the sample.[4]

set.seed(20210906)
y <- rnorm(1000, mean = 35, sd = 2)
qy <- quantile(y, probs = c(0.025, 0.975))
hist(y, probability = TRUE, col = beige, border = cobre,
     main = "N(35, 4): estimated and theoretical densities", xlab = "Value")
abline(v = qy, col = cobre, lty = 2)
lines(density(y), col = cobre, lwd = 2)
curve(dnorm(x, mean = 35, sd = 2), add = TRUE, lty = 3, lwd = 2)
legend("topright", c("Kernel estimate", "Theoretical normal"),
       col = c(cobre, tinta), lty = c(1, 3), bty = "n", cex = 0.7)

The seed supplied by set.seed() allows the pseudorandom sequence to be repeated in a reproducible environment. R versions and random-number-generator settings also matter; sessionInfo() documents the session. A histogram with probability = TRUE displays densities and has unit total area, allowing comparison with density curves.

density(y) estimates a curve from the data using kernels and a bandwidth. It does not know the normal density that generated the sample. Use dnorm() to display that theoretical density. Approximate agreement between the two is a simulation result, not an identity.[5]

A function that simulates, plots, and returns results

The plot.m() function retains the original example’s purpose, adds a line for the mean, checks its inputs, and returns the data and summaries. invisible() avoids automatically printing a lengthy object while still allowing the result to be saved.

plot.m <- function(n, m, s) {
  stopifnot(length(n) == 1L, is.finite(n), n >= 2, n == as.integer(n),
            length(m) == 1L, is.finite(m), length(s) == 1L, is.finite(s), s > 0)
  y <- rnorm(n, mean = m, sd = s)
  qy <- quantile(y, c(0.025, 0.975))
  hist(y, probability = TRUE, col = beige, border = cobre,
       main = paste("Normal sample: n =", n), xlab = "Value")
  abline(v = qy, col = cobre, lty = 2)
  abline(v = mean(y), col = tinta, lwd = 2)
  lines(density(y), col = cobre)
  invisible(list(media = mean(y), sd = sd(y), cuantiles = qy, datos = y))
}
muestra_10 <- plot.m(10, 100, 15)
muestra_10000 <- plot.m(10000, 100, 15)
print(muestra_10[c("media", "sd", "cuantiles")])
print(muestra_10000[c("media", "sd", "cuantiles")])

In the original version, a heading announced n = 10, whereas the call used 10,000. Both sizes are shown here, under the same N(100, 15²) model, as distinct experiments. A larger sample usually gives a more stable description of the population; it does not reduce the population dispersion of the individual observations.

The four families of distribution functions should be distinguished: d* evaluates a density or probability mass, p* a cumulative probability, q* a quantile, and r* generates realizations. Thus, dnorm(), pnorm(), qnorm(), and rnorm() perform different operations on the same distribution family.

05 / Variation across samples

The sampling distribution and standard error of the mean

A population, a sample from it, and a distribution of sample means are different objects. To study the third, repeat the same procedure: draw n observations, calculate their mean, save the result, and start again with another independent sample.

If X₁, …, Xₙ are independent and Xᵢ ∼ N(μ, σ²):X̄ ∼ N(μ, σ²/n)E(X̄) = μ   ·   SD(X̄) = σ/√n

Here the normal distribution of the mean is exact for every n. No asymptotic approximation is required to obtain it. For a non-normal population, the central limit theorem establishes an approximation under its conditions; no universal sample-size threshold guarantees a good approximation for every phenomenon.

n_sim <- 10000L; mu_sim <- 100; sigma_sim <- 15; B <- 1000L
medias <- numeric(B)
for (i in seq_len(B)) {
  sam <- rnorm(n_sim, mean = mu_sim, sd = sigma_sim)
  medias[i] <- mean(sam)
}
print(c(media_teorica = mu_sim, media_simulada = mean(medias),
        ee_teorico = sigma_sim / sqrt(n_sim), ee_simulado = sd(medias)))
qm <- quantile(medias, c(0.025, 0.975))
hist(medias, probability = TRUE, col = beige, border = cobre,
     main = "Distribution of means", xlab = "Sample mean")
abline(v = qm, col = cobre, lty = 2); lines(density(medias), col = cobre)

With σ = 15 and n = 10,000, the theoretical standard error is 0.15. sd(medias) approximates it through simulation; exact agreement is not required because the exercise uses 1,000 repetitions, not infinitely many. The mean of those 1,000 means should be close to 100. Its theoretical Monte Carlo standard error is 0.15/√1000, approximately 0.00474.

numeric(B) preallocates space for B results. Within the for loop, sam contains the complete sample for the current repetition; medias[i] stores only its mean. A loop repeats instructions: it is not itself a function, although it can be written inside one. The analogy with computational orbits may help represent iteration, but it does not imply that the procedure is a dynamical model of the phenomenon being investigated.

Theoretical normal densities centered at 100: observations have standard deviation 15; means of 10000 observations have standard error 0.15.
Figure 02 Theoretical distributions under normality and independence. The horizontal scales differ: the dispersion of the means is one hundred times smaller.

Extension: an initially asymmetric population

An exponential distribution with rate 1 has mean 1 and variance 1. Comparing means from samples of sizes 1, 5, and 30 shows how their shape changes and how their standard deviation approaches 1/√n. This exercise illustrates an approximation process distinct from the exact normality of the previous example.

set.seed(20210907)
medias_exp <- lapply(c(1L, 5L, 30L), function(nn) {
  replicate(4000L, mean(rexp(nn, rate = 1)))
})
names(medias_exp) <- c("n1", "n5", "n30")
print(vapply(medias_exp, mean, numeric(1)))
print(vapply(medias_exp, sd, numeric(1)))

replicate() repeats an expression; lapply() organizes the results for several sizes. The simulation mathematically represents repeated sampling. It does not replace a real experiment or, by itself, establish that a model adequately describes observations from a laboratory, stores, or patients.

06 / Estimation

Confidence intervals for a mean

For an independent sample from a normal population with unknown σ, the statistic (X̄ − μ)/(S/√n) follows a t distribution with n − 1 degrees of freedom. Inverting its central region gives the two-sided interval:

CI₁₋α(μ) = X̄ ± t₁₋α/₂, ₙ₋₁ · S/√nCenter: estimate   ·   Half-width: critical value × standard error

The t distribution arises by combining a standardized normal mean with an independent estimate of its variance. Its tails are heavier than those of the standard normal distribution for finite degrees of freedom; as these increase, it approaches the normal distribution. qt(0.975, n - 1) returns the quantile with 97.5% probability to its left; for a two-sided 95% interval, 2.5% remains in each tail.[6]

n_ventas <- length(ventas)
t_critico <- qt(0.975, df = n_ventas - 1)
ee_ventas <- sd(ventas) / sqrt(n_ventas)
mean(ventas) - t_critico * ee_ventas
mean(ventas) + t_critico * ee_ventas
mean(ventas) + c(-1, 1) * t_critico * ee_ventas

The vector c(-1, 1) constructs the two endpoints simultaneously. The following function generalizes the calculation to confidence level p; this argument is not a test’s p-value. It allows explicit removal of missing values, requires at least two observations, and stops if there is no variation.

ic <- function(x, p = 0.95, na.rm = FALSE) {
  stopifnot(is.numeric(x), length(p) == 1L, is.finite(p), p > 0, p < 1,
            is.logical(na.rm), length(na.rm) == 1L, !is.na(na.rm))
  if (na.rm) x <- x[!is.na(x)]
  if (any(!is.finite(x))) stop("Missing or non-finite data.")
  n_x <- length(x)
  if (n_x < 2L) stop("At least two observations are required.")
  if (sd(x) == 0) stop("There is no sample variation.")
  ee_x <- sd(x) / sqrt(n_x)
  critico <- qt((1 + p) / 2, df = n_x - 1)
  setNames(mean(x) + c(-1, 1) * critico * ee_x, c("lower", "upper"))
}
print(ic(ventas, 0.95)); print(ic(ventas, 0.99))
print(ic(estudios$ipc, 0.95)); print(ic(estudios$menor16, 0.95))
stopifnot(isTRUE(all.equal(unname(ic(ventas)),
                          as.numeric(t.test(ventas)$conf.int))))

For the illustrative data, the 95% CI for mean sales is [177.080, 198.254], whereas the 99% CI is [173.226, 202.108]. Holding the data fixed while increasing confidence raises the critical value and widens the interval. This does not mean that the estimator becomes more robust to extreme observations or model misspecification.

The frequentist interpretation of 95% concerns the procedure: if sampling and interval construction were repeated under the same conditions, 95% of the intervals would contain μ. Once the data have been observed, the parameter is fixed and the particular interval either contains it or does not. Nor does the interval for μ claim that 95% of stores have sales within it.

95% and 99% confidence intervals for mean sales; both contain the null value 180.
Figure 03 Increasing confidence widens the interval for the same data. Both intervals contain 180.

07 / One parameter

The one-sample t-test for a mean

The exercise asks whether population mean sales differ from 180 units per month. We adopt H₀: μ = 180, H₁: μ ≠ 180, and α = 0.05. The reference value needs an external justification—for example, a standard or a prior hypothesis—and should not be selected to obtain a desired result.

tobs = (x̄ − μ₀)/(s/√n)Under H₀ and independent normal sampling: T ∼ tₙ₋₁p = P(|T| ≥ |tobs|) = 2 P(T ≤ −|tobs|)
alpha <- 0.05; mu0 <- 180
n_ventas <- length(ventas); gl <- n_ventas - 1L
ee_ventas <- sd(ventas) / sqrt(n_ventas)
t_obs <- (mean(ventas) - mu0) / ee_ventas
p_manual <- 2 * pt(-abs(t_obs), df = gl)
prueba_una <- t.test(ventas, mu = mu0, alternative = "two.sided",
                    conf.level = 1 - alpha)
print(prueba_una)
print(c(t_manual = t_obs, p_manual = p_manual))
stopifnot(isTRUE(all.equal(p_manual, prueba_una$p.value)))
# Choose a one-sided alternative before examining the results.
prueba_mayor <- t.test(ventas, mu = mu0, alternative = "greater")

The expression using abs() works for either sign of the statistic. pt() evaluates the t cumulative distribution function, not a density. Doubling is justified by that distribution’s symmetry and the two-sided alternative. Using pt(-abs(t_obs), ...) also avoids the subtraction 1 - pt(...), which may lose precision in a very small tail.[6]

Result · Illustrative data

Mean = 187.667; t = 1.511; df = 20; p = 0.1465; 95% CI = [177.080, 198.254].

At the 0.05 level, H₀ is not rejected. Under this test and its assumptions, the data do not provide sufficient evidence against μ = 180. This does not establish that μ is exactly 180 or that a relevant difference is impossible.

A t distribution with 20 degrees of freedom and two tails beyond minus and plus 1.511, with total probability 0.1465.
Figure 04 Total area in the two tails: p ≈ 0.1465. Calculated under H₀, the specified model, and the two-sided alternative.

One-sided tests and duality with confidence intervals

alternative = "greater" tests for a mean greater than μ₀; "less" tests for a smaller mean. The rejection region and the interval returned by R change accordingly. A tail must not be selected after observing the effect’s sign. For corresponding two-sided tests and two-sided intervals, at the same level and under the same procedure, excluding μ₀ is equivalent to rejecting H₀, apart from the convention at the boundary.

The test is not performed “inside” an interval as an independent operation: testing and interval estimation are two related ways of summarizing the same information under the model. The p-value does not replace the interval or the effect’s magnitude in its original units.

08 / Comparison

Constructing groups without losing sight of the original variable

The exercise divides stores into ipc < 17 and ipc ≥ 17. The threshold consistent with the original code is 17; the mention of 117 was an inconsistency in its heading. Creating two categories does not turn the study into an experiment or make an association causal.

umbral <- 17
estudios$ipc1 <- ifelse(estudios$ipc < umbral, 1L, 2L)
estudios$ipc2 <- factor(estudios$ipc1, levels = c(1, 2),
                        labels = c("ipc < 17", "ipc >= 17"))

med_grupos <- tapply(estudios$ventas, estudios$ipc2, mean)
sd_grupos <- tapply(estudios$ventas, estudios$ipc2, sd)
var_grupos <- tapply(estudios$ventas, estudios$ipc2, var)
n_grupos <- table(estudios$ipc2)
stopifnot(length(n_grupos) == 2L, all(n_grupos >= 2L), all(var_grupos > 0))
print(cbind(n = as.numeric(n_grupos), media = med_grupos,
            sd = sd_grupos, varianza = var_grupos))

ifelse() expresses the classification; factor() assigns levels and labels. A grouping variable classifies observations, but is not synonymous with a variable in general. tapply() applies a function to groups defined by factor levels; the technical reference to a ragged array does not mean that a data frame has rows of different lengths.[7]

In the constructed records, the ipc < 17 group contains 10 stores with mean 168.8; the ipc ≥ 17 group contains 11 stores with mean 204.818. The order of the levels fixes the direction of the difference: first minus second.

Boxplots and means with intervals

A boxplot summarizes the median and quartiles; its whiskers depend on the convention used and are not confidence intervals for the mean. Points with confidence bars represent a different object: mean estimates and their uncertainty. gplots::plotmeans() provides this second display and is retained as an option. A diamond-shaped marker does not turn every interval plot into a “diamond plot” with a universal statistical meaning.

plot(ipc ~ ipc2, data = estudios, col = beige, border = cobre,
     xlab = "Group", ylab = "IPC", main = "Classification by IPC")
abline(h = umbral, col = cobre, lty = 2)
boxplot(ventas ~ ipc2, data = estudios, col = beige, border = cobre,
        ylab = "Sales", xlab = "Group", main = "Sales by group")

ic_grupos <- sapply(split(estudios$ventas, estudios$ipc2), ic)
plot(seq_along(med_grupos), med_grupos, pch = 18, xaxt = "n",
     xlim = c(0.5, 2.5), ylim = range(ic_grupos), xlab = "Group", ylab = "Sales",
     main = "Means and 95% confidence intervals")
axis(1, at = 1:2, labels = names(med_grupos))
arrows(1:2, ic_grupos[1, ], 1:2, ic_grupos[2, ],
       angle = 90, code = 3, length = 0.05, col = cobre)
# Optional resource from the original article:
if (requireNamespace("gplots", quietly = TRUE)) {
  gplots::plotmeans(ventas ~ ipc2, data = estudios, connect = FALSE,
                    col = cobre, barcol = cobre)
}

Overlap or non-overlap between two individual 95% intervals does not replace a test of the difference between means. That question requires the standard error of the difference and its own interval. A useful display can accompany the test but does not mechanically replace it.

09 / Two means

Student’s pooled-variance test and Welch’s test

We test H₀: μ₁ − μ₂ = 0 against a two-sided alternative. Both versions require independence between units; they differ in their treatment of group variability. The subtraction order must be preserved when interpreting the sign.

Equal population variances

If σ₁² = σ₂² is justified, the pooled variance weights the sample variances by their degrees of freedom. It is not a simple arithmetic mean, nor is it unconditionally “more precise”: its justification depends on the common-variance model.

sp² = [(n₁ − 1)s₁² + (n₂ − 1)s₂²]/(n₁ + n₂ − 2)SEp = √[sp²(1/n₁ + 1/n₂)]t = [(x̄₁ − x̄₂) − Δ₀]/SEp   ·   df = n₁ + n₂ − 2
dif_medias <- unname(med_grupos[1] - med_grupos[2])
sp2 <- sum((as.numeric(n_grupos) - 1) * var_grupos) / (sum(n_grupos) - 2)
ee_pool <- sqrt(sp2 * sum(1 / as.numeric(n_grupos)))
t_pool <- dif_medias / ee_pool
gl_pool <- sum(n_grupos) - 2
p_pool <- 2 * pt(-abs(t_pool), df = gl_pool)
prueba_pool <- t.test(ventas ~ ipc2, data = estudios, var.equal = TRUE)
print(prueba_pool)

Without imposing equal variances: Welch

Welch estimates the variances separately and uses Welch–Satterthwaite degrees of freedom. These need not be integers. This is the default behavior of t.test() for two independent samples; var.equal = TRUE explicitly requests the preceding version.[8]

SEW = √(s₁²/n₁ + s₂²/n₂)ν = (s₁²/n₁ + s₂²/n₂)² /{(s₁²/n₁)²/(n₁ − 1) + (s₂²/n₂)²/(n₂ − 1)}tW = [(x̄₁ − x̄₂) − Δ₀]/SEW
a_w <- var_grupos / as.numeric(n_grupos)
ee_welch <- sqrt(sum(a_w))
gl_welch <- sum(a_w)^2 / sum(a_w^2 / (as.numeric(n_grupos) - 1))
t_welch <- dif_medias / ee_welch
p_welch <- 2 * pt(-abs(t_welch), df = gl_welch)
prueba_welch <- t.test(ventas ~ ipc2, data = estudios, var.equal = FALSE)
print(prueba_pool); print(prueba_welch)
stopifnot(isTRUE(all.equal(unname(p_pool), prueba_pool$p.value)),
          isTRUE(all.equal(unname(p_welch), prueba_welch$p.value)),
          isTRUE(all.equal(unname(t.test(ventas ~ ipc2, data = estudios)$p.value),
                           prueba_welch$p.value)))

Result · Illustrative data

The difference “ipc < 17 minus ipc ≥ 17” is −36.018 units. Welch gives t ≈ −5.658, df ≈ 18.712, and p ≈ 0.0000198. The pooled-variance version gives p ≈ 0.0000184.

Both rules reject equality of means in this constructed example. The result describes a difference between the defined groups; it does not demonstrate that increasing the ipc indicator causes higher sales. The effect’s magnitude and direction should be reported alongside the interval returned by the test.

The interval for the difference is constructed as (x̄₁ − x̄₂) ± critical value × standard error, using the appropriate degrees of freedom. If the corresponding two-sided interval contains zero, equality is not rejected at the same level. This does not establish equivalence: that question requires substantive margins and a procedure designed to test them.

A preliminary F-test should not serve as an automatic switch between Student and Welch. Failing to reject equality of variances does not establish that they are equal; the full selection strategy changes the procedure’s properties. In this notebook, Welch is the main option, while the pooled version illustrates the common-variance case.

10 / Two variances

The F-test and an interval for a ratio

Comparing dispersion is a different question from comparing means. The general hypothesis is H₀: σ₁²/σ₂² = r₀, with r₀ > 0. For equality, set r₀ = 1. This does not require σ₁² = σ₂² = 1, only that the two variances be equal.

Fobs = (s₁²/s₂²)/r₀Under H₀, with independent normal samples:F ∼ F(n₁ − 1, n₂ − 1)

The F distribution is positive and asymmetric. For the equal-tail two-sided test implemented here, the p-value is twice the smaller tail probability, capped at one. Always doubling pf(F_obs, ...) may produce an incorrect probability, potentially exceeding one.[9]

razon0 <- 1
razon_muestral <- unname(var_grupos[1] / var_grupos[2])
f_obs <- razon_muestral / razon0
gl1 <- as.numeric(n_grupos[1]) - 1; gl2 <- as.numeric(n_grupos[2]) - 1
p_f <- min(1, 2 * min(pf(f_obs, gl1, gl2),
                       pf(f_obs, gl1, gl2, lower.tail = FALSE)))
ic_razon <- razon_muestral / qf(c(1 - alpha/2, alpha/2), gl1, gl2)
prueba_f <- var.test(ventas ~ ipc2, data = estudios, ratio = razon0)
print(prueba_f); print(ic_razon)
stopifnot(isTRUE(all.equal(p_f, prueba_f$p.value)),
          isTRUE(all.equal(ic_razon, as.numeric(prueba_f$conf.int))))
If r̂ = s₁²/s₂², then:CI₁₋α(σ₁²/σ₂²) =[ r̂/F₁₋α/₂;ν₁,ν₂ , r̂/Fα/₂;ν₁,ν₂ ]

The interval estimates the population ratio; it is not symmetric around r̂ on the original scale. If it contains one, equality is not rejected by the corresponding two-sided test. When testing a different ratio, the comparison is with r₀, not necessarily one.

In the illustrative data, the sample ratio is approximately 1.049 and p ≈ 0.9338. Equality of variances is not rejected. This establishes neither their identity nor equality of the means.

11 / Within-pair dependence

Two sleep treatments in the same people

The historical sleep dataset contains 20 observations on ten patients, with two soporific treatments. extra is the increase in sleep hours relative to control; group identifies the treatment and ID the patient. These are not twenty independent people.[11]

Pairing should be constructed by identifier, rather than assuming that two vectors retain the same order. In sleep$group == "1", $ accesses the group column; “1” selects a treatment level and does not mean “column number one.” Selected rows and columns are distinguished by the comma in [rows, columns].

data("sleep", package = "datasets")
A <- sleep[sleep$group == "1", c("ID", "extra")]
B_sleep <- sleep[sleep$group == "2", c("ID", "extra")]
stopifnot(!anyDuplicated(A$ID), !anyDuplicated(B_sleep$ID),
          setequal(A$ID, B_sleep$ID))
pares <- merge(A, B_sleep, by = "ID", suffixes = c("_A", "_B"), sort = TRUE)
stopifnot(nrow(pares) == 10L, all(complete.cases(pares)))

Define Dᵢ = Aᵢ − Bᵢ. The paired test of H₀: μD = 0 is a one-sample t-test applied to the differences:

tpaired = d̄/(sD/√npairs)Degrees of freedom = npairs − 1Var(A − B) = Var(A) + Var(B) − 2 Cov(A, B)

The covariance explains why paired analysis can have different precision from analysis that ignores the pairs. Its advantage depends on the dependence structure. Differences from distinct patients must be independent; for the exact t-test, normality concerns the differences, rather than requiring two separately normal marginal distributions.

dif_sueno <- pares$extra_A - pares$extra_B
prueba_pareada <- t.test(pares$extra_A, pares$extra_B, paired = TRUE)
prueba_diferencias <- t.test(dif_sueno, mu = 0)
prueba_independiente <- t.test(pares$extra_A, pares$extra_B)
print(prueba_pareada); print(prueba_independiente)
print(c(dz = mean(dif_sueno) / sd(dif_sueno)))
stopifnot(isTRUE(all.equal(prueba_pareada$p.value, prueba_diferencias$p.value)))
matplot(1:2, t(as.matrix(pares[c("extra_A", "extra_B")])),
        type = "b", pch = c(1, 16), lty = 1, col = cobre,
        xaxt = "n", xlab = "Treatment", ylab = "Increase in sleep (hours)",
        main = "Paired measurements by patient")
axis(1, at = 1:2, labels = c("A", "B"))
# Patient-index graph retained from the original example:
plot(seq_len(nrow(pares)), pares$extra_A, type = "b", pch = 1,
     col = cobre, ylim = range(pares[c("extra_A", "extra_B")]),
     xlab = "Patient", ylab = "Increase in hours", main = "A and B by patient")
lines(seq_len(nrow(pares)), pares$extra_B, type = "b", pch = 16, lty = 2)
legend("topleft", c("A", "B"), col = c(cobre, tinta), pch = c(1, 16),
       lty = c(1, 2), bty = "n")

Result · Historical sleep dataset

Mean difference A − B = −1.58 hours; t = −4.062; df = 9; p = 0.002833; 95% CI = [−2.460, −0.700] hours.

Treatment B has a greater mean increase in sleep than A in these records. The estimated difference is 1.58 hours in B’s favor. Clinical and causal inference requires considering the historical study’s design and conditions in addition to the calculation.

Paired measurements for ten patients in sleep, connected between treatments A and B; the mean increase is greater under B.
Figure 05 Each connection identifies the same person. The analysis uses ten differences, not twenty independent patients.

The call without paired = TRUE, retained as a teaching comparison, gives p ≈ 0.07939 because it treats the vectors as independent samples. For this design, that call ignores information supplied by the pairing. Analysis is not selected according to which result crosses 0.05: it is selected according to the data’s structure.

The paired standardized difference dz = d̄/sD is approximately −1.285. This definition differs from dividing an independent-groups difference by a pooled standard deviation; when reporting a standardized effect, identify its denominator.

The original graph comparing A and B along a patient index is also retained. That axis represents identifiers, not a time series. A display with one line per patient between treatments makes the pairing clearer. The marginal intervals from plotmeans(extra ~ group) are not the confidence interval for the paired difference.

12 / Research design

Power, detectable differences, and sample size

Power is the probability of rejecting H₀ when a specific alternative is true. If β is the Type II error probability for that effect, power is 1 − β. There is no single power independent of the true effect.

The example’s “resolution” is formalized as a difference that the study should be able to detect with specified power and significance level α. A minimum relevant difference must be justified by the phenomenon: it is not the smallest difference that a test could ever detect, nor a boundary making detection of smaller effects impossible.

One sample: d = δ/σTwo groups with a common variance: d = (μ₁ − μ₂)/σPaired data: dz = μD/σDPower(δ) = 1 − β(δ)

For planning, σ represents assumed variability or an estimate from relevant prior information. Using a pilot estimate introduces uncertainty; several scenarios should be explored. Conventional effect-size values do not replace a substantive justification for δ.

One sample: detecting three units

The original values are retained: δ = 3, σ = 10.2, target power 0.90, and a two-sided alternative. Here α = 0.05 is written explicitly. The effect size is d = 3/10.2 ≈ 0.29412. pwr is an additional package, not a native R database.

# Optional installation, once:
# install.packages("pwr")
# If installed, it can be attached with library(pwr).

if (requireNamespace("pwr", quietly = TRUE)) {
  plan_pwr <- pwr::pwr.t.test(
    n = NULL, d = 3 / 10.2, sig.level = 0.05,
    power = 0.90, type = "one.sample", alternative = "two.sided"
  )
  print(plan_pwr)
  print(ceiling(plan_pwr$n))
}

The result is n ≈ 123.403, requiring 124 complete observations under the adopted model and assumptions. With n = 124, calculated power is approximately 0.90139. Rounding to the nearest integer could leave power below the target; use ceiling().

The power.t.test() function in base R solves the same exercise without installing packages. Its delta argument uses original units, while sd supplies the standard deviation. strict = TRUE includes both rejection regions in the two-sided test and matches the two-sided calculation in pwr.[12]

delta <- 3; sigma_plan <- 10.2; potencia_objetivo <- 0.9
d_plan <- delta / sigma_plan
plan_una <- power.t.test(n = NULL, delta = delta, sd = sigma_plan,
                        sig.level = alpha, power = potencia_objetivo,
                        type = "one.sample", alternative = "two.sided", strict = TRUE)
n_requerido <- ceiling(plan_una$n)
potencia_redondeada <- power.t.test(n = n_requerido, delta = delta,
                                  sd = sigma_plan, sig.level = alpha,
                                  type = "one.sample", strict = TRUE)$power
print(c(n = plan_una$n, entero = n_requerido,
        potencia = potencia_redondeada))

With n = 40, which difference corresponds to power 0.90?

Fixing n = 40 and solving for δ, with the same σ and α, gives δ ≈ 5.3615 units. In the pwr parameterization, solve for d first, then multiply by σ. Calling a result “optimal” requires a cost function or another criterion; this exercise solves a power constraint, not a general optimization problem.

plan_40 <- power.t.test(n = 40, delta = NULL, sd = sigma_plan,
                      sig.level = alpha, power = potencia_objetivo,
                      type = "one.sample", strict = TRUE)
print(plan_40$delta)
# Equivalent with pwr: solve for d = NULL and multiply by sigma_plan.

A planning curve

A sample size is calculated for each difference from two to six units. Holding α, σ, and power fixed, detecting a smaller difference requires more observations. The curve may be displayed continuously before rounding; the final implementation uses integer sizes and therefore has a stepwise form.

deltas <- seq(2, 6, length.out = 30)
n_plan <- numeric(length(deltas)); d_grid <- deltas / sigma_plan
for (i in seq_along(deltas)) {
  n_plan[i] <- power.t.test(delta = deltas[i], sd = sigma_plan,
                          sig.level = alpha, power = potencia_objetivo,
                          type = "one.sample", strict = TRUE)$n
}
plot(deltas, ceiling(n_plan), type = "s", col = cobre, lwd = 2,
     xlab = "Difference to detect", ylab = "Sample size (rounded up)",
     main = "90% power; alpha = 0.05")
A decreasing sample-size curve for differences from two to six units, with 124 observations needed to detect three units at 90% power.
Figure 06 Integer sample sizes, with σ = 10.2, α = 0.05, target power 0.90, and a two-sided one-mean test. No allowance for loss to follow-up is included.

Amylase: two independent groups

The second example retains a reference pooled variance of 290.9 and an effect of interest of 15 units/ml. The standard deviation is √290.9 ≈ 17.056, and d ≈ 0.87947. With α = 0.05 and power 0.90, the two-sided calculation for two equally sized groups gives n ≈ 28.166 per group: round up to 29 per group, 58 in total.

s2p <- 290.9; sp <- sqrt(s2p); delta_amilasa <- 15
d_amilasa <- delta_amilasa / sp
plan_amilasa <- power.t.test(delta = delta_amilasa, sd = sp,
                           sig.level = alpha, power = potencia_objetivo,
                           type = "two.sample", strict = TRUE)
print(c(por_grupo = ceiling(plan_amilasa$n),
        total = 2 * ceiling(plan_amilasa$n)))

if (requireNamespace("pwr", quietly = TRUE)) {
  print(pwr::pwr.t.test(d = d_amilasa, sig.level = alpha,
                       power = 0.90, type = "two.sample"))
}

This calculation corresponds to the model of two independent groups with a common variance; it is not a general Welch power calculation with arbitrary variances and sample sizes. In paired samples, n counts pairs and the relevant standard deviation is that of the differences. Justified allowances for losses or incomplete records should be added; with an expected retention fraction r, an approximate recruitment target is ceiling(complete n/r), subject to the study design.

Calculating β: the noncentral t distribution

For a one-mean test under normality, with true effect δ = μ − μ₀ and σ specified as a planning assumption, T follows a noncentral t distribution with ν = n − 1 and noncentrality parameter λ = δ√n/σ. The test still estimates its standard error using S; knowing σ as a power-calculation assumption does not turn it into a z-test.

c = t₁₋α/₂,ν   ·   λ = δ√n/σβ(δ) = Pν,λ(−c ≤ T ≤ c)Power = Pν,λ(T < −c) + Pν,λ(T > c)
gl_plan <- n_requerido - 1
crit_plan <- qt(1 - alpha/2, gl_plan)
ncp_plan <- delta * sqrt(n_requerido) / sigma_plan
beta_plan <- pt(crit_plan, gl_plan, ncp = ncp_plan) -
             pt(-crit_plan, gl_plan, ncp = ncp_plan)
print(c(beta = beta_plan, potencia = 1 - beta_plan))
stopifnot(abs(1 - beta_plan - potencia_redondeada) < 1e-10)

With n = 124 and the preceding parameters, β ≈ 0.09861 and power ≈ 0.90139. Agreement with the planning function checks the calculation’s foundations. The script verifies this equality numerically.

13 / Scientific judgment

Assumptions, limitations, and reporting results

A function may return a number even when the question is poorly formulated. Analysis quality depends on the relationship among design, model, measurement, and phenomenon. The following criteria organize that assessment without turning it into a mechanical sequence of preliminary tests.

What to assess before interpreting a t-test
AspectReview question
Unit and independenceDoes each row represent a distinct unit? Are there repeated patients, branches of the same chain, clusters, or temporal dependence?
DesignWas there random selection, random assignment, or convenience sampling? To which population can the findings be generalized?
DistributionAre there strong asymmetries, extreme values, or very small samples? For pairs, what do the differences look like?
VariancesIs equality justified by the model? Is Welch preferable without imposing it?
Missing dataWhich records were excluded, why, and with what possible bias?
Analytical decisionsWere hypotheses, tails, thresholds, and primary outcomes defined before searching for significance?

A quantile–quantile plot and inspection of the data can help assess distributional shape. Failing to reject a normality test does not establish normality, especially with small samples. Nor is every minor deviation a reason to abandon all parametric methods: what matters is how sensitive the particular inference is to that deviation.

If the tools change, preserve the question or declare its modification. A rank test is not automatically a test of equality of means. Bootstrap resampling and permutation methods require their own conditions; permuting labels needs a justification of exchangeability and does not, by itself, repair a dependent or biased design.

Significance, geometry, and magnitude

Observations can be represented geometrically as vectors, and means as projections onto the constant direction. In that language, a difference between estimates has magnitude and direction. Statistical significance additionally requires a reference distribution, an extremeness rule, and sampling assumptions. It is not defined solely as a visible change in a geometric pattern.

A small effect may be significant when estimated precisely; an important effect may have a wide interval. A binary decision does not exhaust the evidence. When many tests are performed, define the family of questions and consider appropriate multiplicity control; for example, p.adjust(valores_p, method = "holm") applies Holm’s adjustment to a specified family. There is no need to indiscriminately adjust every calculation presented for teaching purposes.

A way to report the result

A report should state the question and population, design, sample sizes actually analyzed, estimate in original units, interval, method and assumptions, statistic, degrees of freedom, and p-value, together with the limitations. For the sleep example: “The mean difference A − B was −1.58 hours (95% CI: −2.460 to −0.700); paired t-test, t(9) = −4.062, p = 0.002833, n = 10 pairs.” The report should then explain the meaning of that magnitude and how far the design permits generalization.

A very small p-value should be reported with sufficient digits or scientific notation, not as p = 0. A nonsignificant result should include its estimate and uncertainty; it should not be reframed as absence of an effect. A claim of equivalence requires margins and a specific analysis.

14 / Quick reference

A syntax guide and reproducible documents

Understanding the R resources used here
ResourceOperation
<-, =Assignment in the contexts used here; <- is used for assignments and = for named arguments.
c(), numeric()Construct a vector; preallocate a numeric vector.
$, [ , ]Access a column by name; select rows and columns.
~Express a formula. In these examples, the response is on the left and grouping on the right; its interpretation depends on the function receiving it.
factor(), table()Represent categories; count observations by category.
apply(), tapply()Apply over matrix margins; summarize groups defined by factors.
for, seq_len()Repeat instructions; construct a safe sequence of indices.
pt(), qt()Cumulative probability and quantile of a t distribution.
pf(), qf()Cumulative probability and quantile of an F distribution.
library(), package::function()Attach a package; use a function without attaching the entire package.
TRUE, FALSELogical constants. Prefer these to T and F, whose names can be reassigned.

Inspecting a test object with names(prueba_una) identifies its components. prueba_una$p.value, $statistic, $parameter, $conf.int, and $estimate extract results without manually copying them from the console. Each component’s name and interpretation should be checked in the function’s documentation.

R Markdown: results within prose

In an .Rmd file, an R expression enclosed in backticks and preceded by r is evaluated when the document is rendered. It is not an expression that WordPress executes or an instruction to paste unchanged into the R console.

The mean increase in sleep is `r mean(sleep$extra)` hours.
Hay `r nrow(sleep)` registros y `r length(unique(sleep$ID))` patients.

This distinguishes twenty records from ten patients in sleep. The mean of sleep$extra combines the two treatments; a comparison requires treatment-specific means or, in this design, patient-level differences. A reproducible document should also record transformations, exclusion criteria, and software versions.

15 / Executable appendix

Complete reproducible script

The following script assembles every step in order, includes the 21 illustrative records, produces the graphics, and compares manual calculations with R functions through numerical checks. Run it in a clean session. The main sections work with base R; if pwr and gplots are installed, the optional variants also run. Packages are not installed automatically.

To use the original file when it becomes available, change usar_archivo_original to TRUE and adapt importing to its actual format. The printed store results will change; the historical sleep results and planning scenarios do not depend on those records.

Download the R script

View the complete code · R
# FOUNDATIONS OF ESTIMATION AND HYPOTHESIS TESTING IN R
# JOSÉ MAURICIO GÓMEZ JULIÁN
# Revised edition. The 21 records are ILLUSTRATIVE, not recovered original data.
# Run in a clean session. Packages are not installed automatically.

# 1. Data and checks
usar_archivo_original <- FALSE
if (usar_archivo_original) {
  estudios <- read.table("estudios.txt", header = FALSE,
                        col.names = c("menor16", "ipc", "ventas"))
} else {
  estudios <- data.frame(
    menor16 = c(12,15,11,18,14,16,13,19,17,12,20,15,18,14,16,19,13,17,11,20,18),
    ipc = c(11,12,14,16,13,15,10,16,14,12,18,20,17,22,19,24,18,21,23,17,20),
    ventas = c(145,158,172,183,167,192,151,176,164,180,188,205,194,217,201,229,182,212,221,196,208)
  )
}
stopifnot(all(c("menor16", "ipc", "ventas") %in% names(estudios)),
          all(vapply(estudios, is.numeric, logical(1))))
print(names(estudios)); print(nrow(estudios)); print(ncol(estudios))
print(dim(estudios)); str(estudios)
print(colSums(is.na(estudios)))
if (any(!is.finite(as.matrix(estudios)))) {
  stop("There are missing or non-finite values: document their treatment before analysis.")
}
stopifnot(nrow(estudios) >= 2L)
ventas <- estudios$ventas
print(c(media = mean(ventas), sd = sd(ventas), varianza = var(ventas)))
print(apply(estudios, 2, mean)); print(apply(estudios, 2, sd))
print(quantile(ventas)); print(apply(estudios, 2, quantile))
qv <- quantile(ventas, probs = c(0.025, 0.975))
print(qv)

# Colors and base graphics: no additional packages required.
tinta <- "#2c2418"; cobre <- "#8b4513"; beige <- "#d4c8b0"
par(col = tinta, col.axis = tinta, col.lab = tinta, col.main = tinta,
    bg = "#fffdf7", las = 1)
hist(ventas, breaks = 7, col = beige, border = cobre,
     xlab = "Sales", ylab = "Frequency", main = "Sales: illustrative data")
abline(v = qv, col = cobre, lty = 2)

# 2. Simulation: population parameters, quantiles, and density
set.seed(20210906)
y <- rnorm(1000, mean = 35, sd = 2)
qy <- quantile(y, probs = c(0.025, 0.975))
hist(y, probability = TRUE, col = beige, border = cobre,
     main = "N(35, 4): estimated and theoretical densities", xlab = "Value")
abline(v = qy, col = cobre, lty = 2)
lines(density(y), col = cobre, lwd = 2)
curve(dnorm(x, mean = 35, sd = 2), add = TRUE, lty = 3, lwd = 2)
legend("topright", c("Kernel estimate", "Theoretical normal"),
       col = c(cobre, tinta), lty = c(1, 3), bty = "n", cex = 0.7)

plot.m <- function(n, m, s) {
  stopifnot(length(n) == 1L, is.finite(n), n >= 2, n == as.integer(n),
            length(m) == 1L, is.finite(m), length(s) == 1L, is.finite(s), s > 0)
  y <- rnorm(n, mean = m, sd = s)
  qy <- quantile(y, c(0.025, 0.975))
  hist(y, probability = TRUE, col = beige, border = cobre,
       main = paste("Normal sample: n =", n), xlab = "Value")
  abline(v = qy, col = cobre, lty = 2)
  abline(v = mean(y), col = tinta, lwd = 2)
  lines(density(y), col = cobre)
  invisible(list(media = mean(y), sd = sd(y), cuantiles = qy, datos = y))
}
muestra_10 <- plot.m(10, 100, 15)
muestra_10000 <- plot.m(10000, 100, 15)
print(muestra_10[c("media", "sd", "cuantiles")])
print(muestra_10000[c("media", "sd", "cuantiles")])

# 3. Sampling distribution of the mean: loop and theoretical equivalence
n_sim <- 10000L; mu_sim <- 100; sigma_sim <- 15; B <- 1000L
medias <- numeric(B)
for (i in seq_len(B)) {
  sam <- rnorm(n_sim, mean = mu_sim, sd = sigma_sim)
  medias[i] <- mean(sam)
}
print(c(media_teorica = mu_sim, media_simulada = mean(medias),
        ee_teorico = sigma_sim / sqrt(n_sim), ee_simulado = sd(medias)))
qm <- quantile(medias, c(0.025, 0.975))
hist(medias, probability = TRUE, col = beige, border = cobre,
     main = "Distribution of means", xlab = "Sample mean")
abline(v = qm, col = cobre, lty = 2); lines(density(medias), col = cobre)

# Extension: an exponential population with mean 1 and standard deviation 1.
set.seed(20210907)
medias_exp <- lapply(c(1L, 5L, 30L), function(nn) {
  replicate(4000L, mean(rexp(nn, rate = 1)))
})
names(medias_exp) <- c("n1", "n5", "n30")
print(vapply(medias_exp, mean, numeric(1)))
print(vapply(medias_exp, sd, numeric(1)))

# 4. Confidence intervals: a function with input checks
ic <- function(x, p = 0.95, na.rm = FALSE) {
  stopifnot(is.numeric(x), length(p) == 1L, is.finite(p), p > 0, p < 1,
            is.logical(na.rm), length(na.rm) == 1L, !is.na(na.rm))
  if (na.rm) x <- x[!is.na(x)]
  if (any(!is.finite(x))) stop("Missing or non-finite data.")
  n_x <- length(x)
  if (n_x < 2L) stop("At least two observations are required.")
  if (sd(x) == 0) stop("There is no sample variation.")
  ee_x <- sd(x) / sqrt(n_x)
  critico <- qt((1 + p) / 2, df = n_x - 1)
  setNames(mean(x) + c(-1, 1) * critico * ee_x, c("lower", "upper"))
}
print(ic(ventas, 0.95)); print(ic(ventas, 0.99))
print(ic(estudios$ipc, 0.95)); print(ic(estudios$menor16, 0.95))
stopifnot(isTRUE(all.equal(unname(ic(ventas)),
                          as.numeric(t.test(ventas)$conf.int))))

# 5. One-sample t-test: manual and automatic calculations
alpha <- 0.05; mu0 <- 180
n_ventas <- length(ventas); gl <- n_ventas - 1L
ee_ventas <- sd(ventas) / sqrt(n_ventas)
t_obs <- (mean(ventas) - mu0) / ee_ventas
p_manual <- 2 * pt(-abs(t_obs), df = gl)
prueba_una <- t.test(ventas, mu = mu0, alternative = "two.sided",
                    conf.level = 1 - alpha)
print(prueba_una)
print(c(t_manual = t_obs, p_manual = p_manual))
stopifnot(isTRUE(all.equal(p_manual, prueba_una$p.value)))
# Choose a one-sided alternative before examining the results.
prueba_mayor <- t.test(ventas, mu = mu0, alternative = "greater")

# 6. Grouping and summaries. Illustrative threshold, not optimized.
umbral <- 17
estudios$ipc1 <- ifelse(estudios$ipc < umbral, 1L, 2L)
estudios$ipc2 <- factor(estudios$ipc1, levels = c(1, 2),
                        labels = c("ipc < 17", "ipc >= 17"))
plot(ipc ~ ipc2, data = estudios, col = beige, border = cobre,
     xlab = "Group", ylab = "IPC", main = "Classification by IPC")
abline(h = umbral, col = cobre, lty = 2)
boxplot(ventas ~ ipc2, data = estudios, col = beige, border = cobre,
        ylab = "Sales", xlab = "Group", main = "Sales by group")
med_grupos <- tapply(estudios$ventas, estudios$ipc2, mean)
sd_grupos <- tapply(estudios$ventas, estudios$ipc2, sd)
var_grupos <- tapply(estudios$ventas, estudios$ipc2, var)
n_grupos <- table(estudios$ipc2)
stopifnot(length(n_grupos) == 2L, all(n_grupos >= 2L), all(var_grupos > 0))
print(cbind(n = as.numeric(n_grupos), media = med_grupos,
            sd = sd_grupos, varianza = var_grupos))
ic_grupos <- sapply(split(estudios$ventas, estudios$ipc2), ic)
plot(seq_along(med_grupos), med_grupos, pch = 18, xaxt = "n",
     xlim = c(0.5, 2.5), ylim = range(ic_grupos), xlab = "Group", ylab = "Sales",
     main = "Means and 95% confidence intervals")
axis(1, at = 1:2, labels = names(med_grupos))
arrows(1:2, ic_grupos[1, ], 1:2, ic_grupos[2, ],
       angle = 90, code = 3, length = 0.05, col = cobre)
# Optional resource from the original article:
if (requireNamespace("gplots", quietly = TRUE)) {
  gplots::plotmeans(ventas ~ ipc2, data = estudios, connect = FALSE,
                    col = cobre, barcol = cobre)
}

# 7. Independent means: pooled variance and Welch
dif_medias <- unname(med_grupos[1] - med_grupos[2])
sp2 <- sum((as.numeric(n_grupos) - 1) * var_grupos) / (sum(n_grupos) - 2)
ee_pool <- sqrt(sp2 * sum(1 / as.numeric(n_grupos)))
t_pool <- dif_medias / ee_pool
gl_pool <- sum(n_grupos) - 2
p_pool <- 2 * pt(-abs(t_pool), df = gl_pool)
prueba_pool <- t.test(ventas ~ ipc2, data = estudios, var.equal = TRUE)
a_w <- var_grupos / as.numeric(n_grupos)
ee_welch <- sqrt(sum(a_w))
gl_welch <- sum(a_w)^2 / sum(a_w^2 / (as.numeric(n_grupos) - 1))
t_welch <- dif_medias / ee_welch
p_welch <- 2 * pt(-abs(t_welch), df = gl_welch)
prueba_welch <- t.test(ventas ~ ipc2, data = estudios, var.equal = FALSE)
print(prueba_pool); print(prueba_welch)
stopifnot(isTRUE(all.equal(unname(p_pool), prueba_pool$p.value)),
          isTRUE(all.equal(unname(p_welch), prueba_welch$p.value)),
          isTRUE(all.equal(unname(t.test(ventas ~ ipc2, data = estudios)$p.value),
                           prueba_welch$p.value)))

# 8. F-test and variance-ratio interval
razon0 <- 1
razon_muestral <- unname(var_grupos[1] / var_grupos[2])
f_obs <- razon_muestral / razon0
gl1 <- as.numeric(n_grupos[1]) - 1; gl2 <- as.numeric(n_grupos[2]) - 1
p_f <- min(1, 2 * min(pf(f_obs, gl1, gl2),
                       pf(f_obs, gl1, gl2, lower.tail = FALSE)))
ic_razon <- razon_muestral / qf(c(1 - alpha/2, alpha/2), gl1, gl2)
prueba_f <- var.test(ventas ~ ipc2, data = estudios, ratio = razon0)
print(prueba_f); print(ic_razon)
stopifnot(isTRUE(all.equal(p_f, prueba_f$p.value)),
          isTRUE(all.equal(ic_razon, as.numeric(prueba_f$conf.int))))

# 9. Paired samples: align by ID, rather than assuming row order
data("sleep", package = "datasets")
A <- sleep[sleep$group == "1", c("ID", "extra")]
B_sleep <- sleep[sleep$group == "2", c("ID", "extra")]
stopifnot(!anyDuplicated(A$ID), !anyDuplicated(B_sleep$ID),
          setequal(A$ID, B_sleep$ID))
pares <- merge(A, B_sleep, by = "ID", suffixes = c("_A", "_B"), sort = TRUE)
stopifnot(nrow(pares) == 10L, all(complete.cases(pares)))
dif_sueno <- pares$extra_A - pares$extra_B
prueba_pareada <- t.test(pares$extra_A, pares$extra_B, paired = TRUE)
prueba_diferencias <- t.test(dif_sueno, mu = 0)
prueba_independiente <- t.test(pares$extra_A, pares$extra_B)
print(prueba_pareada); print(prueba_independiente)
print(c(dz = mean(dif_sueno) / sd(dif_sueno)))
stopifnot(isTRUE(all.equal(prueba_pareada$p.value, prueba_diferencias$p.value)))
matplot(1:2, t(as.matrix(pares[c("extra_A", "extra_B")])),
        type = "b", pch = c(1, 16), lty = 1, col = cobre,
        xaxt = "n", xlab = "Treatment", ylab = "Increase in sleep (hours)",
        main = "Paired measurements by patient")
axis(1, at = 1:2, labels = c("A", "B"))
# Patient-index graph retained from the original example:
plot(seq_len(nrow(pares)), pares$extra_A, type = "b", pch = 1,
     col = cobre, ylim = range(pares[c("extra_A", "extra_B")]),
     xlab = "Patient", ylab = "Increase in hours", main = "A and B by patient")
lines(seq_len(nrow(pares)), pares$extra_B, type = "b", pch = 16, lty = 2)
legend("topleft", c("A", "B"), col = c(cobre, tinta), pch = c(1, 16),
       lty = c(1, 2), bty = "n")

# 10. Power and sample size using base R
delta <- 3; sigma_plan <- 10.2; potencia_objetivo <- 0.9
d_plan <- delta / sigma_plan
plan_una <- power.t.test(n = NULL, delta = delta, sd = sigma_plan,
                        sig.level = alpha, power = potencia_objetivo,
                        type = "one.sample", alternative = "two.sided", strict = TRUE)
n_requerido <- ceiling(plan_una$n)
potencia_redondeada <- power.t.test(n = n_requerido, delta = delta,
                                  sd = sigma_plan, sig.level = alpha,
                                  type = "one.sample", strict = TRUE)$power
plan_40 <- power.t.test(n = 40, delta = NULL, sd = sigma_plan,
                      sig.level = alpha, power = potencia_objetivo,
                      type = "one.sample", strict = TRUE)
deltas <- seq(2, 6, length.out = 30)
n_plan <- numeric(length(deltas)); d_grid <- deltas / sigma_plan
for (i in seq_along(deltas)) {
  n_plan[i] <- power.t.test(delta = deltas[i], sd = sigma_plan,
                          sig.level = alpha, power = potencia_objetivo,
                          type = "one.sample", strict = TRUE)$n
}
plot(deltas, ceiling(n_plan), type = "s", col = cobre, lwd = 2,
     xlab = "Difference to detect", ylab = "Sample size (rounded up)",
     main = "90% power; alpha = 0.05")
# Amylase: two independent groups, equal sizes, and a common variance.
s2p <- 290.9; sp <- sqrt(s2p); delta_amilasa <- 15
d_amilasa <- delta_amilasa / sp
plan_amilasa <- power.t.test(delta = delta_amilasa, sd = sp,
                           sig.level = alpha, power = potencia_objetivo,
                           type = "two.sample", strict = TRUE)
print(c(d = d_plan, n_continuo = plan_una$n, n_requerido = n_requerido,
        potencia_redondeada = potencia_redondeada,
        delta_con_n40 = plan_40$delta, d_amilasa = d_amilasa,
        n_amilasa_por_grupo = plan_amilasa$n,
        n_amilasa_redondeado = ceiling(plan_amilasa$n)))

# Original version using the optional pwr package:
# install.packages(c("pwr", "gplots")) # run only if you wish to install these packages
if (requireNamespace("pwr", quietly = TRUE)) {
  plan_pwr <- pwr::pwr.t.test(n = NULL, d = d_plan, sig.level = alpha,
                            power = potencia_objetivo, type = "one.sample",
                            alternative = "two.sided")
  plan_pwr40 <- pwr::pwr.t.test(n = 40, d = NULL, sig.level = alpha,
                              power = potencia_objetivo, type = "one.sample")
  plan_pwr_amilasa <- pwr::pwr.t.test(n = NULL, d = d_amilasa,
                                   sig.level = alpha, power = potencia_objetivo,
                                   type = "two.sample")
  print(plan_pwr); print(plan_pwr40$d * sigma_plan); print(plan_pwr_amilasa)
  stopifnot(abs(plan_pwr$n - plan_una$n) < 0.001,
            abs(plan_pwr_amilasa$n - plan_amilasa$n) < 0.001)
}

# 11. Exact power for a mean under normality: noncentral t
gl_plan <- n_requerido - 1
crit_plan <- qt(1 - alpha/2, gl_plan)
ncp_plan <- delta * sqrt(n_requerido) / sigma_plan
beta_plan <- pt(crit_plan, gl_plan, ncp = ncp_plan) -
             pt(-crit_plan, gl_plan, ncp = ncp_plan)
print(c(beta = beta_plan, potencia = 1 - beta_plan))
stopifnot(abs(1 - beta_plan - potencia_redondeada) < 1e-10)

# R Markdown: write this inside an .Rmd, not as standalone R code:
# The mean increase in sleep is `r mean(sleep$extra)` hours.
# The number of records is `r nrow(sleep)`; there are `r length(unique(sleep$ID))` patients.

# 12. Verifiable summary and session information
cat("\nVERIFIED_SUMMARY\n")
print(list(
  media_ventas = mean(ventas), sd_ventas = sd(ventas), cuantiles_ventas = qv,
  ic95_ventas = ic(ventas), ic99_ventas = ic(ventas, 0.99),
  una_muestra = c(t = unname(prueba_una$statistic), gl = gl, p = prueba_una$p.value),
  grupos = c(diferencia = dif_medias, t_welch = unname(t_welch),
             gl_welch = unname(gl_welch), p_welch = p_welch, p_pool = p_pool,
             f = f_obs, p_f = p_f),
  sueno = c(diferencia = mean(dif_sueno), sd_diferencias = sd(dif_sueno),
            t = unname(prueba_pareada$statistic), p = prueba_pareada$p.value,
            ic_inf = prueba_pareada$conf.int[1], ic_sup = prueba_pareada$conf.int[2],
            p_independiente = prueba_independiente$p.value),
  planificacion = c(n_una = plan_una$n, delta40 = plan_40$delta,
                    n_amilasa = plan_amilasa$n)))
sessionInfo()

16 / Sources

References and documentation

The linked documentation allows the arguments, distributions, and assumptions to be checked. Functions are cited where they are used; reference links do not replace the explanations developed in the text.

  1. Wasserstein, R. L., and Lazar, N. A. (2016). The ASA’s Statement on p-Values: Context, Process, and Purpose. The American Statistician, 70(2), 129–133. doi:10.1080/00031305.2016.1154108.
  2. R Core Team. Apply Functions Over Array Margins: apply().
  3. R Core Team. Sample Quantiles: quantile().
  4. R Core Team. The Normal Distribution: dnorm(), pnorm(), qnorm(), rnorm().
  5. R Core Team. Kernel Density Estimation: density().
  6. R Core Team. The Student t Distribution: dt(), pt(), qt(), rt().
  7. R Core Team. Apply a Function Over a Ragged Array: tapply().
  8. R Core Team. Student’s t-Test: t.test().
  9. R Core Team. The F Distribution: df(), pf(), qf(), rf().
  10. R Core Team. F Test to Compare Two Variances: var.test(). See also fligner.test().
  11. R Core Team. Student’s Sleep Data: sleep. The documentation refers to Cushny and Peebles (1905) and Student (1908).
  12. R Core Team. Power Calculations for One and Two Sample t Tests: power.t.test().
  13. Champely, S., and contributors. pwr: Basic Functions for Power Analysis, version 1.3-0. Package manual. Calculations use Cohen’s (1988) effect-size parameterization.
  14. The gplots team. gplots: Various R Programming Tools for Plotting Data. Manual for plotmeans() and other functions.
  15. R Core Team. Histogram: hist(); Adjust P-values for Multiple Comparisons: p.adjust().

Original publication and readings cited in its comments

This edition revises Fundamentos generales del proceso de estimación y prueba de hipótesis en R Studio. Parte II, código en R Studio con comentarios (2021), originally published in Spanish under the pseudonym Isadore Nabi. The author’s name for this edition is JOSÉ MAURICIO GÓMEZ JULIÁN.

The resources cited in the original comments are retained as provenance references: Minitab’s explanation of t-values and t-tests; Statology’s guide to the t distribution in R; Boston University’s power and sample-size material; and StatTrek’s F-distribution calculator. They form part of the text’s history; official documentation supports this edition’s corrections to syntax and functions.

The thematic connections to the author’s Spanish-language texts Análisis teórico de la función cuantil en R Studio [Theoretical Analysis of the Quantile Function in RStudio] and Modelos lineales generalizados [Generalized Linear Models] are also retained. The original publication mentions Part I on statistical theory; this notebook develops the foundations used here without attributing to that document content that has not been explicitly incorporated.

Estimate · Test · Interpret

Computational precision becomes valuable when it supports a well-defined question, explicit assumptions, and a substantive interpretation of uncertainty.

JOSÉ MAURICIO GÓMEZ JULIÁN

Read the source publication


Discover more from Marxist Philosophy of Science

Subscribe to get the latest posts sent to your email.

Follow the blogSeguí al blog

Comments

Leave a Comment/Deja un Comentario