11 Inferential Statistics in R

🎯 Learning goals

After working through Tutorial 11, you’ll be able to…

  • understand the logic of inferential statistics (e.g., hypotheses, p-values)
  • apply functions for inferential analyses, including correlation, t-test, and chi-square test

Inferential statistics

So far, we have used descriptive statistics to summarize our sample — for example, calculating the mean trust in government among journalists. But in most research, we are not just interested in the sample itself. We want to know whether what we observe in the sample also holds for the broader population.

This is where inferential statistics come in. While descriptive statistics describe your sample, inferential statistics help us make assumptions about whether those findings hold for the population.

In this tutorial, we will cover three inferential tests from the tidycomm package:

  • correlate(): tests the relationship between two metric variables (Pearson correlation)
  • t_test(): tests whether the mean of a metric variable differs across two groups
  • crosstab(): tests the relationship between two categorical variables (Chi-Square test)

We will once again use the WoJ dataset, which contains survey data from N = 1,200 journalists across five countries.

# we load the necessary packages
library(tidycomm)
library(tidyverse)

# loads the WoJ dataset shipped with tidycomm
data_woj <- tidycomm::WoJ

# we inspect the data
data_woj |>
  as_tibble()
# A tibble: 1,200 × 15
   country   reach employment temp_contract autonomy_selection autonomy_emphasis
   <fct>     <fct> <chr>      <fct>                      <dbl>             <dbl>
 1 Germany   Nati… Full-time  Permanent                      5                 4
 2 Germany   Nati… Full-time  Permanent                      3                 4
 3 Switzerl… Regi… Full-time  Permanent                      4                 4
 4 Switzerl… Local Part-time  Permanent                      4                 5
 5 Austria   Nati… Part-time  Permanent                      4                 4
 6 Switzerl… Local Freelancer <NA>                           4                 4
 7 Germany   Local Full-time  Permanent                      4                 4
 8 Denmark   Nati… Full-time  Permanent                      3                 3
 9 Switzerl… Local Full-time  Permanent                      5                 5
10 Denmark   Nati… Full-time  Permanent                      2                 4
# ℹ 1,190 more rows
# ℹ 9 more variables: ethics_1 <dbl>, ethics_2 <dbl>, ethics_3 <dbl>,
#   ethics_4 <dbl>, work_experience <dbl>, trust_parliament <dbl>,
#   trust_government <dbl>, trust_parties <dbl>, trust_politicians <dbl>

The logic of inferential statistics

Before running any test, it helps to understand the three-step logic behind inferential statistics. Note: Since this is a introduction to R class, this is a oversimplified explanation of inferential statistics. Explaining this more (and better) is something we do not have time for in this class - but hopefully at a later time of your studies.

Step 1: Formulate a hypothesis. A hypothesis describes an expected relationship between two (or more) variables in the population. Hypotheses are falsifiable — we can test them. They can be directional (e.g., the more X, the more Y) or non-directional (e.g., X and Y are related).

Step 2: Test the hypothesis. We use an inferential test to evaluate the hypothesis using our sample data.

Step 3: Decide whether the hypothesis is supported. Based on the test result, we assess whether there is sufficient evidence to (for now) support our hypothesis.

The key metric for this decision is the p-value. It tells us how likely our observed data (or something more extreme) would be if there were actually no effect in the population. By convention in our class:

  • If p < .05: we consider the result statistically significant and find (for now) evidence for our hypothesis.
  • If p > .05: we consider the result not statistically significant and (for now) do not find sufficient evidence for our hypothesis.

Pearson correlation

A Pearson correlation tests the relationship between two metric variables. It tells us both the direction and the strength of the relationship:

  • A positive correlation coefficient (r > 0) means the two variables increase together.
  • A negative correlation coefficient (r < 0) means one variable increases as the other decreases.

We use the correlate() function from tidycomm. Let’s test the following hypothesis:

H1: The more work experience journalists have, the more critical of the government they become.

The variables we need are work_experience (metric) and trust_government (metric, scale 1–5).

data_woj |>
  correlate(work_experience, trust_government)
# A tibble: 1 × 6
  x               y                      r    df      p     n
* <chr>           <chr>              <dbl> <int>  <dbl> <int>
1 work_experience trust_government -0.0708  1185 0.0146  1187

The output gives us several columns to focus on. The column r is the Pearson correlation coefficient — here, r = -0.07, indicating a small negative relationship: the more work experience journalists have, the slightly less they trust the government. The column p gives us the p-value. Since p = 0.015 < .05, the result is statistically significant.

Reporting: There was a significant negative correlation between journalists’ years of work experience and their trust in the government, r(1185) = -.07, p < .05. We find evidence for H1.

T-test

A t-test tests whether the mean of a metric variable differs significantly across two groups. It is used when one variable is metric and the other is categorical with exactly two categories.

Key things to look at in the output:

  • M and SD for each group: descriptive statistics showing the means and standard deviations per group.
  • t and df: the test statistic and degrees of freedom.
  • p: the p-value — if p < .05, the group difference is statistically significant.
  • d: Cohen’s d, a measure of effect size.
  • Levene_p and var_equal: a check of whether the two groups have equal variance. If var_equal = TRUE, this assumption holds; if not, R automatically corrects for it.

We use the t_test() function from tidycomm. Let’s test the following hypothesis:

H2: The mean work experience of journalists differs across their type of employment (full-time vs. part-time).

data_woj |>
  t_test(employment, work_experience)
# A tibble: 1 × 12
  Variable     `M_Full-time` `SD_Full-time` `M_Part-time` `SD_Part-time` Delta_M
* <chr>            <num:.3!>      <num:.3!>     <num:.3!>      <num:.3!> <num:.>
1 work_experi…        17.534         10.653        16.952         10.992   0.582
# ℹ 6 more variables: t <num:.3!>, df <dbl>, p <num:.3!>, d <num:.3!>,
#   Levene_p <dbl>, var_equal <chr>

Reporting: Journalists with full-time employment had more work experience (M = 17.53, SD = 10.65) compared to journalists with part-time employment (M = 16.95, SD = 10.99). However, this difference is not significant, t(1015) = 0.572, p = 0.568. We reject H2.

Chi-Square test

A Chi-Square test examines the relationship between two categorical variables. Instead of comparing means, it compares counts and percentages across groups to assess whether the distribution of one variable differs systematically across the categories of another.

Key things to look at in the output:

  • Percentages per group: descriptive statistics showing how values are distributed across categories.
  • Chi-square value and df: the test statistic and degrees of freedom.
  • p: the p-value — if p < .05, the association between the two variables is statistically significant.
  • V: Cramér’s V, a measure of effect size for categorical associations.

We use the crosstab() function from tidycomm. Let’s test the following hypothesis:

H3: There is a difference in the types of contracts provided to journalists across countries.

data_woj |>
  
  # remove missing values for contract type
  filter(!is.na(temp_contract)) |>
  
  # calculate the chi-square test
  crosstab(country, temp_contract, percentages = TRUE, chi_square = TRUE)
# A tibble: 2 × 6
  temp_contract Austria Denmark Germany Switzerland     UK
* <fct>           <dbl>   <dbl>   <dbl>       <dbl>  <dbl>
1 Permanent      0.979   0.907   0.951       0.941  0.978 
2 Temporary      0.0211  0.0929  0.0486      0.0591 0.0225
# Chi-square = 15.950, df = 4, p = 0.003, V = 0.126

The output shows the percentage of permanent and temporary contracts for each country. We can see that Denmark has the highest share of temporary contracts (9.3%) compared to the other countries.

Reporting: There was a significant association between country and contract type, χ²(4) = 15.95, p < .05, Cramér’s V = 0.13. The proportion of temporary contracts varied across countries, with Denmark showing the highest share of temporary employment. We find evidence for H3.

🤓 Smart Hacks

Just as we used the report package in Tutorial 10 to automatically write up descriptive statistics, we can use it here to generate readable, APA-style summaries of inferential test results. The knitr package enables us to nicely print the output as a table.

The report() function produces a formatted, human-readable description of your test result. It works with base R test functions (cor.test(), t.test()) rather than tidycomm, but follows the same tidyverse pipe logic. The underlying statistics are identical — only the syntax differs slightly.

Here, we use with() to keep the code pipe-friendly. We then convert the output into a nicely formatted table using as.data.frame() and kable():

# correlation: work experience and trust in government
data_woj |>
  with(cor.test(work_experience, trust_government)) |>
  report() |>
  as.data.frame() |>
  kable(caption = "Automated inferential reporting: Pearson correlation",
        digits = 2)
Automated inferential reporting: Pearson correlation
Parameter1 Parameter2 r CI CI_low CI_high t df_error p Method Alternative
work_experience trust_government -0.07 0.95 -0.13 -0.01 -2.44 1185 0.01 Pearson’s product-moment correlation two.sided

💡 Take-Aways

  • Correlation between metric variables: correlate()
  • Testing whether means differ across two groups: t_test()
  • Chi-square test for two categorical variables: crosstab() with chi_square = TRUE

🎲 Quiz

🎲 Question 1

Which of the following statements about inferential statistics are correct?

📚 More tutorials on this

You still have questions? The following resources can help: