library(inferr)

Inferential statistics allows us to make generalizations about populations using data drawn from the population. We use them when it is impractical or impossible to collect data about the whole population under study and instead, we have a sample that represents the population under study and using inferential statistics technique, we make generalizations about the population from the sample. inferr builds upon the solid set of statistical tests provided in stats package by including additional data types as inputs, expanding and restructuring the test results.

The inferr package:

As of version 0.1, inferr includes a select set of parametric and non-parametric statistical tests which are listed below:

These tests are described in more detail in the following sections.

One Sample t Test

A one sample t-test is used to determine whether a sample of observations comes from a population with a specific mean. The observations must be continuous, independent of each other, approximately distributed and should not contain any outliers.

Example

Using the hsb data, test whether the average of write differs significantly from 50.

infer_os_t_test(hsb, write, mu = 50, alternative = 'all')

Paired t test

A paired (samples) t-test is used when you want to compare the means between two related groups of observations on some continuous dependent variable. In a paired sample test, each subject or entity is measured twice. It can be used to evaluate the effectiveness of training programs or treatments. If the dependent variable is dichotomous, use the McNemar test.

Examples

Using the hsb data, test whether the mean of read is equal to the mean of write.

# Lower Tail Test
infer_ts_paired_ttest(hsb, read, write, alternative = 'less')

# Test all alternatives
infer_ts_paired_ttest(hsb, read, write, alternative = 'all')

Two Independent Sample t Test

An independent samples t-test is used to compare the means of a normally distributed continuous dependent variable for two unrelated groups. The dependent variable must be approximately normally distributed and the cases/subjects in the two groups must be different i.e. a subject in one group cannot also be a subject of the other group. It can be used to answer whether:

Example

Using the hsb data, test whether the mean for write is the same for males and females.

infer_ts_ind_ttest(hsb, female, write, alternative = 'all')

One Sample Test of Proportion

One sample test of proportion compares proportion in one group to a specified population proportion.

Examples

Using hsb data, test whether the proportion of females is 50%.

# Using Variables
infer_os_prop_test(hsb, female, prob = 0.5)

Using Calculator

# Calculator
infer_os_prop_test(200, prob = 0.5, phat = 0.3)

Two Sample Test of Proportion

Two sample test of proportion performs tests on the equality of proportions using large-sample statistics. It tests that a categorical variable has the same proportion within two groups or that two variables have the same proportion.

Examples

Using Variables

Using the treatment data, test equality of proportion of two treatments

# Using Variables
infer_ts_prop_test(treatment, treatment1, treatment2, alternative = 'all')

Use Grouping Variable

Using the treatment2 data, test whether outcome has same proportion for male and female

# Using Grouping Variable
infer_ts_prop_group(treatment2, outcome, female, alternative = 'all')

Using Calculator

Test whether the same proportion of people from two batches will pass a review exam for a training program. In the first batch of 30 participants, 30% passed the review, whereas in the second batch of 25 participants, 50% passed the review.

# Calculator
infer_ts_prop_calc(n1 = 30, n2 = 25, p1 = 0.3, p2 = 0.5, alternative = 'all')

One Sample Variance Test

One sample variance comparison test compares the standard deviation (variances) to a hypothesized value. It determines whether the standard deviation of a population is equal to a hypothesized value. It can be used to answer the following questions:

Examples

Using the mtcars data, compare the standard deviation of mpg to a hypothesized value.

# Lower Tail Test
infer_os_var_test(mtcars, mpg, 0.3, alternative = 'less')

# Test all alternatives
infer_os_var_test(mtcars, mpg, 0.3, alternative = 'all')

Two Sample Variance Test

Two sample variance comparison tests equality of standard deviations (variances). It tests that the standard deviation of a continuous variable is same within two groups or the standard deviation of two continuous variables is equal.

Example

Use Grouping Variable

Using the mtcars data, compare the standard deviation in miles per gallon for automatic and manual vehicles.

# Using Grouping Variable
infer_ts_var_test(hsb, read, group_var = female, alternative = 'all')

Using Variables

Using the hsb data, compare the standard deviation of reading and writing scores.

# Using Variables
infer_ts_var_test(hsb, read, write, alternative = 'all')

Binomial Probability Test

A one sample binomial test allows us to test whether the proportion of successes on a two-level categorical dependent variable significantly differs from a hypothesized value.

Examples

Using the hsb data, test whether the proportion of females and males are equal.

# Using variables
infer_binom_test(hsb, female, prob = 0.5)

Using Calculator

# calculator
infer_binom_calc(32, 16, prob = 0.5)

ANOVA

The one-way analysis of variance (ANOVA) is used to determine whether there are any statistically significant differences between the means of two or more independent (unrelated) groups. It tests the null hypothesis that samples in two or more groups are drawn from populations with the same mean values. It cannot tell you which specific groups were statistically significantly different from each other but only that at least two groups were different and can be used only for numerical data.

Examples

Using the hsb data, test whether the mean of write differs between the three program types.

infer_oneway_anova(hsb, write, prog)

Chi Square Goodness of Fit Test

A chi-square goodness of fit test allows us to compare the observed sample distribution with expected probability distribution. It tests whether the observed proportions for a categorical variable differ from hypothesized proportions. The proportion of cases expected in each group of categorical variable may be equal or unequal. It can be applied to any univariate distribution for which you can calculate the cumulative distribution function. It is applied to binned data and the value of the chi square test depends on how the data is binned. For the chi square approximation to be valid, the sample size must be sufficiently large.

Example

Using the hsb data, test whether the observed proportions for race differs significantly from the hypothesized proportions.

# basic example
infer_chisq_gof_test(hsb, race, c(20, 20, 20 , 140))

Continuity Correction

# using continuity correction
infer_chisq_gof_test(hsb, race, c(20, 20, 20 , 140), correct = TRUE)

Chi Square Test of Independence

A chi-square test is used when you want to test if there is a significant relationship between two nominal (categorical) variables.

Examples

Using the hsb data, test if there is a relationship between the type of school attended (schtyp) and students' gender (female).

infer_chisq_assoc_test(hsb, female, schtyp)

Using the hsb data, test if there is a relationship between the type of school attended (schtyp) and students' socio economic status (ses).

infer_chisq_assoc_test(hsb, schtyp, ses)

Levene's Test

Levene's test is used to determine if k samples have equal variances. It is less sensitive to departures from normality and is an alternative to Bartlett's test. This test returns Levene's robust test statistic and the two statistics proposed by Brown and Forsythe that replace the mean in Levene's formula with alternative location estimators. The first alternative replaces the mean with the median and the second alternative replaces the mean with the 10% trimmed mean.

Examples

Use Grouping Variable

Using the hsb data, test whether variance in reading score is same across race.

# Using Grouping Variable
infer_levene_test(hsb, read, group_var = race)

Using Variables

Using the hsb data, test whether variance is equal for reading, writing and social studies scores.

# Using Variables
infer_levene_test(hsb, read, write, socst)

Cochran's Q Test

Cochran's Q test is an extension to the McNemar test for related samples that provides a method for testing for differences between three or more matched sets of frequencies or proportions. It is a procedure for testing if the proportions of 3 or more dichotomous variables are equal in some population. These outcome variables have been measured on the same people or other statistical units.

Example

The exam data set contains scores of 15 students for three exams (exam1, exam2, exam3). Test if three exams are equally difficult.

infer_cochran_qtest(exam, exam1, exam2, exam3)

McNemar Test

McNemar test is a non parametric test created by Quinn McNemar and first published in Psychometrika in 1947. It is similar to a paired t test but applied to a dichotomous dependent variable. It is used to test if a statistically significant change in proportions have occurred on a dichotomous trait at two time points on the same population. It can be used to answer whether:

Examples

Using the hsb data, test if the proportion of students in himath and hiread group is equal.

hb <- hsb
hb$himath <- ifelse(hsb$math > 60, 1, 0)
hb$hiread <- ifelse(hsb$read > 60, 1, 0)
infer_mcnemar_test(hb, himath, hiread)
himath <- ifelse(hsb$math > 60, 1, 0)
hiread <- ifelse(hsb$read > 60, 1, 0)
infer_mcnemar_test(table(himath, hiread))

Perform the above test using matrix as input.

infer_mcnemar_test(matrix(c(135, 18, 21, 26), nrow = 2))

Runs Test for Randomness

Runs Test can be used to decide if a data set is from a random process. It tests whether observations of a sequence are serially independent i.e. whether they occur in a random order by counting how many runs there are above and below a threshold. A run is defined as a series of increasing values or a series of decreasing values. The number of increasing, or decreasing, values is the length of the run. By default, the median is used as the threshold. A small number of runs indicates positive serial correlation; a large number indicates negative serial correlation.

Examples

We will use runs test to check regression residuals for serial correlation.

# basic example
infer_runs_test(hsb, read)

# drop values equal to threshold
infer_runs_test(hsb, read, drop = TRUE)

# recode data in binary format
infer_runs_test(hsb, read, split = TRUE)

# use mean as threshold
infer_runs_test(hsb, read, mean = TRUE)

# threshold to be used for counting runs
infer_runs_test(hsb, read, threshold = 0)


rsquaredacademy/inferr documentation built on June 2, 2021, 8:52 a.m.