Hypothesis tests and confidence intervals

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

bitest

Test a binary success probability exactly

Stata

bitest b = 0.5

R Package:stats

stats::binom.test(sum(data$b),nrow(data),p=0.5)

Differs from Stata Returns an exact two-sided binomial test. The code assumes the displayed binary sample has no missing values; Stata additionally prints lower- and upper-tail probabilities.

bitesti

Run an exact binomial test from counts

Stata

bitesti 20 7 0.5

R Package:stats

stats::binom.test(7,20,p=0.5)

Differs from Stata Uses an exact two-sided binomial test from the supplied count and total. R reports one htest result; Stata additionally displays both one-sided probabilities.

ci

Compute a Student-t confidence interval for a mean

Stata

ci means y

R Package:stats

stats::t.test(data$y)$conf.int

Differs from Stata Returns only the selected confidence limits, not Stata sample-size and point-estimate columns. Match nonmissing sample size, confidence level and the explicit interval method.

4 more examples and alternatives

Compute a Clopper-Pearson proportion interval

Stata

ci proportions b, exact

R Package:stats

stats::binom.test(sum(data$b),nrow(data))$conf.int

Differs from Stata Returns only the selected confidence limits, not Stata sample-size and point-estimate columns. Match nonmissing sample size, confidence level and the explicit interval method.

Compute a Wilson score interval for a proportion

Stata

ci proportions b, wilson

R Package:stats

stats::prop.test(sum(data$b),nrow(data),correct=FALSE)$conf.int

Differs from Stata Returns only the selected confidence limits, not Stata sample-size and point-estimate columns. Match nonmissing sample size, confidence level and the explicit interval method.

Compute a normal-theory variance interval

Stata

ci variances y

R Package:stats

(length(data$y)-1)*stats::var(data$y)/stats::qchisq(c(0.975,0.025),df=length(data$y)-1)

Differs from Stata Returns only the selected confidence limits, not Stata sample-size and point-estimate columns. Match nonmissing sample size, confidence level and the explicit interval method.

Compute a normal-theory standard-deviation interval

Stata

ci variances y, sd

R Package:stats

sqrt((length(data$y)-1)*stats::var(data$y)/stats::qchisq(c(0.975,0.025),df=length(data$y)-1))

Differs from Stata Returns only the selected confidence limits, not Stata sample-size and point-estimate columns. Match nonmissing sample size, confidence level and the explicit interval method.

cii

Compute a mean interval from summary statistics

Stata

cii means 50 10 2

R Package:stats

10+c(-1,1)*stats::qt(0.975,49)*2/sqrt(50)

Differs from Stata Computes confidence limits from the displayed summary inputs. The R result omits Stata sample-size and point-estimate columns; the stated method and confidence level determine the interval.

Compute an exact proportion interval from counts

Stata

cii proportions 20 7, exact

R Package:stats

stats::binom.test(7,20)$conf.int

Differs from Stata Computes confidence limits from the displayed summary inputs. The R result omits Stata sample-size and point-estimate columns; the stated method and confidence level determine the interval.

ksmirnov

Compare two empirical distribution functions

Stata

ksmirnov y, by(g)

R Package:stats

stats::ks.test(data$y[data$g=="a"],data$y[data$g=="b"],exact=FALSE)

Differs from Stata The two-sample form uses a grouping variable; a one-sample test instead requires a fully specified reference CDF.

ktau

Test Kendall rank association

Stata

ktau y x

R Package:stats

stats::cor.test(data$y,data$x,method="kendall",exact=FALSE)

Differs from Stata Uses the asymptotic Kendall test. Tie adjustments and the reported tau variant must match; R returns one htest object rather than Stata full concordance summaries.

kwallis

Test equality of distributions across groups

Stata

kwallis y, by(g)

R Package:stats

stats::kruskal.test(y~g,data=data)

Differs from Stata Computes the tie-corrected Kruskal-Wallis chi-square approximation. R returns an htest object without Stata group rank-sum summaries.

median

Run a two-group median test

Stata

median y, by(g)

R Package:stats

stats::chisq.test(table(data$g,data$y>stats::median(data$y)),correct=FALSE)

Differs from Stata Tests independence of group and the above-pooled-median indicator. The example assigns observations tied at the median to the lower category; align Stata ties() handling before comparison.

nptrend

Test an ordered trend in proportions

Stata

nptrend b, group(g) carmitage

R Package:stats

stats::prop.trend.test(c(10,18,28),c(40,40,40),score=c(1,2,3))

Differs from Stata Illustrates aggregated successes and denominators at the three ordered group scores.

oneway

Compare group means with Bonferroni adjustment

Stata

oneway y g, bonferroni

R Package:stats

stats::pairwise.t.test(data$y,data$g,p.adjust.method="bonferroni",pool.sd=TRUE)

Differs from Stata Uses the ordinary equal-variance one-way model or pooled pairwise t tests as shown. R object layout differs and does not reproduce every Stata tabulate column or comparison display.

1 more examples and alternatives

Fit one-way analysis of variance

Stata

oneway y g, tabulate

R Package:stats

stats::aov(y~factor(g),data=data)

Differs from Stata Uses the ordinary equal-variance one-way model or pooled pairwise t tests as shown. R object layout differs and does not reproduce every Stata tabulate column or comparison display.

pcorr

Compute the partial correlation of y and x controlling z

Stata

pcorr y x z

R Package:stats

stats::cor(stats::residuals(stats::lm(y~z,data=data)),stats::residuals(stats::lm(x~z,data=data)))

Differs from Stata This returns the y-x partial correlation; Stata pcorr also reports semipartial correlations for each listed predictor.

prtest

Run an uncorrected large-sample one-proportion test

Stata

prtest b == 0.5

R Package:stats

stats::prop.test(sum(data$b),nrow(data),p=0.5,correct=FALSE)

Differs from Stata Disables the R continuity correction for the normal-approximation proportion test. R reports a chi-square statistic whose square root is the absolute Stata z; signed direction requires the group order.

1 more examples and alternatives

Compare two proportions without continuity correction

Stata

prtest b, by(g)

R Package:stats

stats::prop.test(tapply(data$b,data$g,sum),as.vector(table(data$g)),correct=FALSE)

Differs from Stata Disables the R continuity correction for the normal-approximation proportion test. R reports a chi-square statistic whose square root is the absolute Stata z; signed direction requires the group order.

prtesti

Compare proportions from aggregate sample sizes

Stata

prtesti 100 0.4 120 0.5

R Package:stats

stats::prop.test(c(40,60),c(100,120),correct=FALSE)

Differs from Stata Disables continuity correction. R returns a chi-square test; Stata reports a signed z and one-sided probabilities, although the two-sided p-values correspond for the same counts.

ranksum

Compare independent distributions with the rank-sum test

Stata

ranksum y, by(g)

R Package:stats

stats::wilcox.test(y~g,data=data,exact=FALSE,correct=FALSE)

Differs from Stata Aligns the requested exact or uncorrected asymptotic Wilcoxon calculation. Tie handling, group ordering and reported rank-sum versus U-statistic conventions must be aligned.

1 more examples and alternatives

Use exact rank-sum inference without ties

Stata

ranksum y, by(g) exact

R Package:stats

stats::wilcox.test(y~g,data=data,exact=TRUE)

Differs from Stata Aligns the requested exact or uncorrected asymptotic Wilcoxon calculation. Tie handling, group ordering and reported rank-sum versus U-statistic conventions must be aligned.

sdtest

Test one normal population standard deviation

Stata

sdtest y == 3

R Package:stats

{ statistic <- (length(data$y)-1)*stats::var(data$y)/3^2; c(chisq=statistic,p.value=2*min(stats::pchisq(statistic,11),stats::pchisq(statistic,11,lower.tail=FALSE))) }

Differs from Stata Uses a normal-theory variance-ratio or chi-square test. R returns test components in an htest object or vector; Stata also reports one-sided alternatives and additional confidence limits.

Compare two unpaired sample variances

Stata

sdtest y == x

R Package:stats

stats::var.test(data$y,data$x)

Differs from Stata Uses a normal-theory variance-ratio or chi-square test. R returns test components in an htest object or vector; Stata also reports one-sided alternatives and additional confidence limits.

Compare variances of two independent normal samples

Stata

sdtest y, by(g)

R Package:stats

stats::var.test(y~g,data=data)

Differs from Stata Uses a normal-theory variance-ratio or chi-square test. R returns test components in an htest object or vector; Stata also reports one-sided alternatives and additional confidence limits.

sdtesti

Compare variances from sample sizes and standard deviations

Stata

sdtesti 30 2 40 3

R Package:stats

{ f <- 2^2/3^2; c(F=f,p.value=2*min(stats::pf(f,29,39),stats::pf(f,29,39,lower.tail=FALSE))) }

Differs from Stata Computes the two-sided normal-theory variance-ratio test from the supplied summaries. The R vector omits Stata confidence intervals and one-sided probability columns.

signrank

Use a paired signed-rank test with ties

Stata

signrank y = x

R Package:stats

stats::wilcox.test(data$y,data$x,paired=TRUE,exact=FALSE,correct=FALSE)

Differs from Stata Uses the paired Wilcoxon asymptotic calculation without continuity correction. R omits zero differences; ties and small-sample variance conventions can affect the reference approximation.

signtest

Test the median paired difference using signs

Stata

signtest y = x

R Package:stats

{ d <- data$y-data$x; stats::binom.test(sum(d>0),sum(d!=0),p=0.5) }

Differs from Stata Drops zero paired differences and applies an exact two-sided binomial test. The displayed R interval concerns the probability of a positive difference, not a confidence interval for the paired median.

spearman

Test Spearman rank association with ties

Stata

spearman y x

R Package:stats

stats::cor.test(data$y,data$x,method="spearman",exact=FALSE)

Differs from Stata Uses the asymptotic Spearman test explicitly. R and Stata p-value approximations can differ, especially with ties; compare the rank correlation on the same complete pairs.

swilk

Test univariate normality with Shapiro-Wilk

Stata

swilk y

R Package:stats

stats::shapiro.test(data$y)

Differs from Stata Computes the Shapiro-Wilk W test; R permits 3 to 5000 nonmissing observations. Stata also displays transformed statistics and uses its own approximation and sample-size rules.

symmetry

Test symmetry in a matched binary table

Stata

symmetry rater1 rater2

R Package:stats

stats::mcnemar.test(table(data$rater1,data$rater2),correct=FALSE)

Differs from Stata For a 2 by 2 table this is the uncorrected McNemar test; larger-table symmetry needs a different procedure.

tabi

Run a Pearson test from immediate cell counts

Stata

tabi 12 8 \ 5 15, chi2

R Package:stats

stats::chisq.test(matrix(c(12,8,5,15),nrow=2,byrow=TRUE),correct=FALSE)

Differs from Stata Builds the stated table by rows and disables continuity correction for Pearson chi-square. R returns an htest object rather than Stata contingency-table output.

tabulate

Test association in a two-way count table

Stata

tabulate g b, chi2

R Package:stats

stats::chisq.test(table(data$g,data$b),correct=FALSE)

Differs from Stata table excludes missing observations and unused factor levels can affect dimensions. The R htest object provides the selected Pearson or Fisher test without Stata row and column percentage displays.

1 more examples and alternatives

Run a Fisher exact test for a two-way table

Stata

tabulate g b, exact

R Package:stats

stats::fisher.test(table(data$g,data$b))

Differs from Stata table excludes missing observations and unused factor levels can affect dimensions. The R htest object provides the selected Pearson or Fisher test without Stata row and column percentage displays.

ttest

Test a mean against a specified value

Stata

ttest y == 8

R Package:stats

stats::t.test(data$y,mu=8)

Differs from Stata Selects pooled, Welch or paired inference explicitly. R returns an htest object and may reverse the reported difference when factor-level order differs; match group order and the nonmissing sample.

4 more examples and alternatives

Compute a 90 percent interval for a mean

Stata

ttest y == 8, level(90)

R Package:stats

stats::t.test(data$y,mu=8,conf.level=0.90)

Differs from Stata Selects pooled, Welch or paired inference explicitly. R returns an htest object and may reverse the reported difference when factor-level order differs; match group order and the nonmissing sample.

Test paired mean differences

Stata

ttest y == x

R Package:stats

stats::t.test(data$y,data$x,paired=TRUE)

Differs from Stata Selects pooled, Welch or paired inference explicitly. R returns an htest object and may reverse the reported difference when factor-level order differs; match group order and the nonmissing sample.

Compare two independent means with pooled variance

Stata

ttest y, by(g)

R Package:stats

stats::t.test(y~g,data=data,var.equal=TRUE)

Differs from Stata Selects pooled, Welch or paired inference explicitly. R returns an htest object and may reverse the reported difference when factor-level order differs; match group order and the nonmissing sample.

Compare two means using Welch unequal-variance inference

Stata

ttest y, by(g) unequal

R Package:stats

stats::t.test(y~g,data=data,var.equal=FALSE)

Differs from Stata Selects pooled, Welch or paired inference explicitly. R returns an htest object and may reverse the reported difference when factor-level order differs; match group order and the nonmissing sample.

ttesti

Run a one-mean t test from summary statistics

Stata

ttesti 30 10 2 8

R Package:stats

{ t <- (10-8)/(2/sqrt(30)); c(t=t,df=29,p.value=2*stats::pt(-abs(t),29)) }

Differs from Stata Computes the one-sample t statistic, degrees of freedom and two-sided p-value from the supplied summaries. The R vector omits the confidence interval and one-sided probabilities.