Practical 1 - Exploring relationships and fitting linear models
2022-08-04
1 Intro
Intended learning outcomes:
produce scatterplots of report quality;
fit linear regression models by using
lm
;interpret least squares estimates of model parameters;
calculate least squares estimates of model parameters for simple linear regression.
1.1 Introduction
In the lectures we have examined relationships between variables using scatterplots. We have considered how to fit a linear model, through estimating the parameters such as \(\beta\) in the linear model \(Y_i = \alpha + \beta x_i +\epsilon _i\), by the method of least squares or maximum likelihood. In today’s lab we shall use RStudio to explore and fit relationships using a variety of linear model examples.
We assume that the data take the form \((y_i, x_i)\) for \(i = 1, . . . , n\) and we will model the following relationship \(Y_i = f(x_i) + \epsilon_i\).
1.2 Getting started (optional)
This course assumes basic knowledge of R
and RStudio. The starting point is opening RStudio by going to:
File > New > R script
This will open a new screen for you to type into.
Go to:
File > Save as
and save the script file into your home directory. Now, start by setting the working directory in RStudio to
the directory in which you have stored both the data and R
script. One way of doing this is to go to:
Session > Set Working Directory > To Source File Location
This will point RStudio towards this directory to enable us to use files within that directory.
It is useful to annotate your script file using comments so that you know what has been done when you look at it in the future. Comments can be added to a script file by starting the line with a # symbol.