Practical 3 - Inferential Statistics in R

Practical 3 – Inferential Statistics

  • This session focuses on using R for data screening and hypothesis testing.
  • Covers relationships between two groups, correlation analysis, and simple linear regression.
  • Complete the practical in the lab or in your free time.

Learning Objectives

  1. Create a dataframe from a CSV file using read.csv() command.
  2. Plot a histogram and Q-Q plot to explore data distribution.
  3. Test for non-normality.
  4. Test for homogeneity of variances.
  5. Conduct a t-test for independent samples.
  6. Conduct a Mann-Whitney test for independent samples.
  7. Create a scatterplot of two continuous variables.
  8. Conduct a correlation analysis.
  9. Build a Simple Linear Regression.

Initial Steps in RStudio

  • Set the working directory via Session -> Set working directory -> Choose directory.
  • Navigate to your …./RStudio-stats subfolder.
  • Verify the working directory by checking if the files in the Files tab match those in your RStudio-stats subfolder.

Practical Steps

  • Answer questions for each step in a Word file named “Answers to practical 3”.
  • Copy & paste plots into the Word file.
  • Create a new R Script named “Practical 3” for your code.
  • Understand the datasets and the study's aims.
  • Run each line of code sequentially, typing the code instead of copy-pasting.
STEP 1: Creating a Dataframe
  • Context: Air quality data from Manchester, focusing on PM10 measurements in µg/m3 from two stations between January 1 and December 31, 2022.

  • The study aims to investigate the similarity in PM10 levels and trends between the two stations and to determine if a relationship exists between PM10 concentrations at the two stations.

  • The dataset is stored in “Manchester_PM10.csv”.

  • Download the dataset and save it in your working directory.

  • Import the file into RStudio using the following code:

    ############## Open a csv file from the console ###################
    Manchester_aq<-read.csv("Manchester_PM10.csv", header = TRUE, stringsAsFactors = TRUE)
    #inspect the type of object
    class(Manchester_aq)
    
  • Questions:

    • How many cases and variables does the dataset have?
    • Specify the type of variable and the scale of measurement for each.
    • What are the groups you would like to compare?
STEP 2: Summary Statistics
  • Perform summary statistics to describe the data.

    ################ Summary Statistics ###########
    #get the mean, median and quartiles along with the range, maximum and minimum values
    summary(Manchester_aq$Manchester1_PM10)
    summary(Manchester_aq$Manchester2_PM10)
    #get the mean for each set of variable
    mean(Manchester_aq$Manchester1_PM10)
    mean(Manchester_aq$Manchester2_PM10)
    #get the standard deviation for each set of variables
    sd(Manchester_aq$Manchester1_PM10)
    sd(Manchester_aq$Manchester2_PM10)
    #get the min and max values for each group
    range(Manchester_aq$Manchester1_PM10)
    range(Manchester_aq$Manchester2_PM10)
    
  • Question:

    • Write a narrative describing the data, including basic information.
STEP 3: Plotting Histograms
  • Plot histograms for the variables using the hist() function.

    ################ PLOT A HISTOGRAM ###########
    # for PM10 – Manchester-1
    hist_Manchester1_PM10<-hist(Manchester_aq$Manchester1_PM10, xlab= "Microgram per cubic meter", ylab=”Frequency”, main= "Concentration of PM10 on Manchester-1 AQ station", col="skyblue")
    # for PM10 – Manchester-2
    hist_Manchester2_PM10<-hist(Manchester_aq$Manchester2_PM10, xlab= "Microgram per cubic meter", ylab=”Frequency”, main= "Concentration of PM10 on Manchester-2 AQ station", col="darkred ")
    
  • Question:

    • Copy and paste the histograms into your answer document and include a caption.
STEP 4: Hypothesis Testing
  • Objective: Determine if there is a significant difference between the mean concentrations of PM10 measured in both stations.
4.1: The Null Hypothesis
  • Questions:
    • Write the logical hypothesis and its null equivalent (logical null hypothesis) in an IF-THEN statement.
    • Write the null statistical hypothesis.
4.2: Data Screening
  • Screen data to check assumptions of the statistical test.
4.2.1: Testing for Normality
  • Test the null hypothesis that the data comes from a normal distribution.

  • Null Hypothesis (Ho): Data comes from a population with normal distribution

  • Alternative Hypothesis (Hi): Data does not come from a population with normal distribution

  • Set confidence level at 95%, so α=0.05\alpha = 0.05.

    ################### Test for non-normality ######################
    # use the Shapiro-Wilk test for PM10 – Manchester-1
    shapiro.test(Manchester_aq$Manchester1_PM10)
    # use the Shapiro-Wilk test for PM10 – Manchester-2
    shapiro.test(Manchester_aq$Manchester2_PM10)
    
  • If the p-value > α\alpha, fail to reject Ho (accept the null hypothesis); the data comes from a normal distribution.

  • Question:

    • Is the data from both groups normally distributed? Write your results in narrative form, including the test statistic and p-value.
  • Create Q-Q plots to visualize if the data comes from a normally distributed population.

    ################### QQ-plots ######################
    #specify that we want the plots together. Build an empty table to put  #the plots in. So, 1 row and 2 columns.
    par(mfrow=c(1,2))
    # create Q-Q plot for PM10 - Manchester-1
    qqnorm(Manchester_aq$Manchester1_PM10, main="Q-Q Plot for PM10  (Manchester-1)")
    qqline(Manchester_aq$Manchester1_PM10, col = "hotpink4", lwd = 2)
    # create Q-Q plot for PM10 – Manchester-2
    qqnorm(Manchester_aq$Manchester2_PM10, main="Q-Q Plot for PM10  (Manchester-2)")
    qqline(Manchester_aq$Manchester2_PM10, col = "red", lwd = 2)
    
    par(mfrow=c(1,2))
    qqPlot(Manchester_aq$Manchester1_PM10, main="Q-Q Plot for PM10  (Manchester-1)", ylab="Sample Quantiles")
    qqPlot(Manchester_aq$Manchester2_PM10, main="Q-Q Plot for PM10  (Manchester-2)", ylab="Sample Quantiles")
    
  • If points fall approximately along the reference line, assume normality; otherwise, assume non-normality.

  • Copy and paste the plots in your answer file and reference them in the narrative describing the results for the normality test.

4.2.2: Testing for Homogeneity of Variances
  • Test if the variances of the groups are homogeneous using Fligner-Killeen's test.

  • Alternative tests: F-Test, Bartlett’s test, Levene’s test.

  • Null Hypothesis (Ho): the variances are homogeneous (equal)

  • Alternative Hypothesis (Hi): the variances are heterogeneous (different)

    #################### Test for Homoscesdasticity ####################
    # run the Filgner-Killeen test
    fligner.test(Manchester1_PM10 ~ Manchester2_PM10, data = Manchester_aq )
    
  • If p-value > α\,\alpha (0.05), conclude that no significant difference was observed between the variances.

  • Question:

    • Are the variances homogeneous? Write results in narrative form, including the test statistic, degrees of freedom, and p-value.
  • If variances are homogeneous, proceed to perform a t-test.

4.3: Student's t-test
  • Perform a t-test for independent samples.

    #################### Independent samples t-test####################
    #perform the t-test
    t.test(Manchester_aq$Manchester1_PM10, Manchester_aq$Manchester2_PM10, data=Manchester_aq, var.equal=TRUE, paired=FALSE)
    
  • If the p-value > α\,\alpha (0.05), accept the Null Hypothesis; otherwise, accept the alternative hypothesis.

  • Questions:

    • Do you fail to accept or reject the Null Hypothesis? Write the results of your t-test in a narrative form and remember to include the value of the test statistic, degrees of freedom, and p-value.
    • What argument(s) would you change in the t.test function and how if you would like to perform a t-test for unpaired samples?
  • Learn how to do a Mann-Whitney test when a dataset fails the normality test.

    #################### Mann-Whitney U test####################
    # Mann-Whitney U test
    wilcox.test(Manchester_aq$Manchester1_PM10, Manchester_aq$Manchester2_PM10, data=Manchester_aq)
    
  • If the p-value > α\,\alpha (0.05), accept the Null Hypothesis; otherwise, accept the alternative hypothesis.

Step 5: Scatterplot
  • Create a scatterplot of two continuous variables.

    ###to clear the par column formatting above
    par(mfrow=c(1,1))
    ################### Scatterplot ######################
    plot(Manchester_aq$Manchester1_PM10, Manchester_aq$Manchester2_PM10, xlab = “Level of PM10 in Manchester-1 station”, ylab = “Level of PM10 in Manchester-2 station”, main = “Scatter plot of PM10  (Manchester-1) vs PM10  (Manchester-2)“)
    model<-lm(Manchester_aq$Manchester2_PM10 ~ Manchester_aq$Manchester1 _PM10, data = Manchester_aq)
    abline(model, col = "blue", lwd=2)
    
  • Question:

    • Describe the pattern or relation observed in the scatterplot in narrative form; also paste the scatter plot in your answer document.
5.1: Correlation Analysis
  • Find out if the observed relation is strong and not just due to chance.

  • Run a correlation test with a confidence level of 95% (α=0.05\alpha = 0.05).

  • Null Hypothesis (Ho): there is no correlation between the variables, r = 0

  • Alternative Hypothesis (Hi): the variables are correlated, r≠ 0

    ################### Correlation test ####################
    #run a correlation test assuming our data is normally distributed
    cor.test(x= Manchester_aq$Manchester1_PM10, y = Manchester_aq$Manchester2_PM10)
    #run the same analysis, now assuming the data is not normally distributed
    cor.test(x= Manchester_aq$Manchester1_PM10, y = Manchester_aq$Manchester2_PM10, exact=FALSE, method = "spearman")
    
  • Question:

    • Do you fail to accept or reject your Null Hypothesis? Write the results in a narrative form and remember to include the value of the test statistic, degrees of freedom, and p-value.
STEP 6: Simple Linear Regression
  • Use simple linear regression to model the relationship, assuming the variables are strongly linearly correlated.

    #################### Simple Linear regression ####################
    #build the model
    slr<-lm(formula = Manchester2_PM10 ~ Manchester1_PM10  , data = Manchester_aq)
    #explore the model output
    summary(slr)
    
  • Question:

    • Write the results in a narrative form and remember to include an interpretation for the r2 value.