R

Basic code structure for Linear models

Linear models with numeric dependent variable and no clustering variable

 

Linear model used

Relevant chapters

Code

Comparing two means from two independent samples

Linear model with

  • Numeric dependent variable
  • Dichotomous independent variable

Chapter 6

  • model < - lm ( y ∼ x , data = dataset )         
  • model % >% tidy ()


Comparing more than two means from independent samples

Linear model with

  • Numeric dependent variable
  • Categorical independent variable

Chapter 6

If x is a factor variable

  • model < - lm ( y ∼ x , data = dataset )
  • model % >% anova % >% tidy ()

If x is stored as a numeric variable

  • model < - lm ( y ∼ as . factor ( x ) , data = dataset ) 
  • model % >% anova % >% tidy ()


Testing the interaction effect of two categorical variables on  a numeric dependent variable

Linear model with

  • Numeric dependent variable
  • Two independent categorical variables

Chapter 9

If x and z are factor variables

  • model < - lm ( y ∼ x + z + x :z , data = dataset )
  • model % >% anova % >% tidy ()


Testing the interaction effect of two numeric variables on a numeric dependent variable

Linear model with

  • Numeric dependent variable
  • Two numeric independent variables

Chapter 9

If x and z are numeric variables

  • model < - lm ( y ∼ x + z + x :z , data = dataset )
  • model % >% tidy ()


Testing the interaction effect of one independent numeric variable and one numeric dependent variable

Linear model with

  • Numeric dependent variable
  • One numeric independent variable
  • One categorical independent variable

Chapter 9 

  • model < - lm ( y ∼ x + z + x :z , data = dataset )
  • model % >% anova % >% tidy ()


Regression

Linear model with

  • Numeric dependent variable
  • Numeric or dummy independent variable

Chapter 4 and 6.5

  • model < - lm ( y ∼ x + z , data = dataset)
  • model % >% tidy ()


MODELS WITH A NUMERIC DEPENDENT VARIABLE AND A CLUSTERING VARIABLE (DUE TO REPEATED MEASUREMENTS) 



New approach

Relevant chapters

Code

Comparing two means from two related samples

Linear mixed model with

  • Numeric dependent variable
  • Dichotomous independent variable
  • Clustering variable “id”

Chapter 12

  • library ( lme4 )
  • model < - lmer ( y ∼ x + (1| id ) , data = dataset )
  • model % >% tidy ()


Comparing more than two means from related samples

Linear mixed model with

  • Numeric dependent variable
  • Categorical independent variable
  • Clustering variable “id”

Chapter 13

  • library ( lme4 )
  • model < - lmer ( y ∼ x + (1| id ) , data = dataset )
  • model % >% tidy ()


MODEL WITH A DUMMY OR A COUNT DEPENDENT VARIABLE



New approach

Relevant chapters

Code

Logistic regression

Generalized linear model with

  • Dummy dependent variable
  • Numeric/dummy/categorical independent variable

Chapter 15

  • model < - glm ( y ∼ x , family = binomial , data = dataset )
  • model % >% tidy ()


Testing the independence of two categorical variables

Generalized linear model with

  • Dependent variable that represents counts
  • Independent categorical variables

Chapter 16

  • model < - glm ( y ∼ x + z + x :z , family = poisson , data = dataset )
  • model % >% tidy ()


Manuals & Websites

Please keep in mind that when working with R it is normal to search for the specific codes needed. 

The UT provides a manual with different R codes for several types of analyses. It is mainly aimed at codes used in the statistics courses of several Bachelor programmes but can be a good starting point.

If you need information on how to analyse data using linear models you can have a look at this book

If you have questions regarding Bayesian statistics please have a look at this book. It also provides information on how to set up R and how to install libraries from cran but also other sources.