Introduction to Linear Model in R
A statistical or mathematical model that is used to formulate a relationship between a dependent variable and single or multiple independent variables called as, linear model in R. The criteria is that the variables involved in the formation of model meet certain assumptions as necessary prerequisites prior model building and that the model has certain important elements as its parts, which are formula, data, subset, weights, method, model, offset etc. It is not necessary that all have to be used every time, but only those that are sufficient and essential in the given context.
Advantages of Linear Model:
- Helps us to understand the type and nature of the data.
- Helps us to predict the data.
- Helps us to make statistical inferences from data.
Now we will learn about linear regression basically it is a statistical method used to create these models. The main objective of this model is to explain the relationship between the dependent variable and the independent variable.
Syntax of Linear Model in R
Here is the syntax of the linear model in R which is given below.
lm(formula, data, subset, weights, na.action,
method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE,
singular.ok = TRUE,offset, ...)
Here are the parameters of the linear model which are explained below:
- Formula: Here we have to enter the variables of our dataset, basically, those variables where we are planning to trace out whether any relationship exists between them or not. The format should be fixed like (Dependent variable ~ Independent variables). Eg (Distance ~ Speed), (Demand~Price), etc.
- Data: It is used when we have to pass an optional list of data, data frame or environment.
- Subset: It helps us to define the data when we have to use a subset of the observations.
- Weights: It accepts only numeric vector or “NULL” command. If it is not null, “WLS (Weighted least squares)” is used with weights or if Null then OLS (ordinary least squares) is used.
- Na.action: It will give the instruction of what should be done when the data points have NA values, like na.fail, na.omit, na.exclude, etc.
- Method: It is used for fitting.
- Model: It is a logical vector if it is TRUE the corresponding components of the fit are returned.
- X: It is a logical vector if it is TRUE the corresponding components of the fit are returned.
- Y: It is a logical vector if it is TRUE the corresponding components of the fit are returned.
- qr: It is a logical vector if it is TRUE the corresponding components of the fit are returned.
- ok: It is also a logical vector if it is FALSE then singular fits are the error.
- Offset: It can be NULL, numerical vector or matrix. This is used to specify an a priori known component to be included in the linear predictor during fitting.
Types of Linear Model in R
Let’s now discuss different types of linear models which are as follows:
1. Simple Linear Regression
This model helps us to explain a relationship between one dependent variable and one independent variable. With the help of it, we can also predict the data, by providing the input values. In general, the dependent variable is also known as the response variable, regressand, observed variable, responding variable, measured variable, explained variable, experimental variable, outcome variable, and/or output variable). And independent variable known as a controlled variable, regressors, explanatory variable, manipulated variable, exposure variable, and/or input variable. The equation for the simple linear regression model is:
Where β1 is an intercept, β2 is a slope and ϵ is an error term. We will use the “USArrest” data set.
|Murder arrests (per 100,000)||Assault arrests (per 100,000)||Percent urban population||Rape arrests (per 100,000)|
Now we will find the relationship between the Assault variable and the Urban population.
>dataset = USArrests
>Linear_relationship1 = lm(Assault~ UrbanPop, data=dataset)
Equation looks like:
Now we have intercept and slope also, Assault = 73.08 + 1.49(UrbanPop). Here we have a linear model equation, we have to supply the inputs in the form of “UrbanPop”, and the model equation will automatically predict the value of “Assualt” for us. Let’s take another example of this model, now we will run this model on Murder variable and Urban Population variable.
>Linear_relationship2= lm(Murder~ UrbanPop, data=dataset)
Equation looks like:
Now we have intercept and slope also, Murder= 6.41594 + 0.02093(UrbanPop). Here we have a linear model equation, we have to supply the inputs in the form of “UrbanPop”, and the model equation will automatically predict the value of “Murder” for us.
2. Multiple Linear Regression
In this model, we will have one dependent variable and multiple independent variables. Multiple independent variables are used in this model to predict one dependent variable. Let’s take an example of this model. Here also we will use the “USArrests” dataset.
Dependent variable = Urban Population
Independent variable = Assault, Rape, and Murder
>Multiple_Linear_Relationship = lm(UrbanPop~ Assault+Rape+Murder , data=dataset)
The equation looks like:
Now we have intercept and slope also, UrbanPop= 52.8419 + 0.0519(Assault) + 0.6984(Rape) – 1.4115(Murder)
The linear model generally works around two parameters: one is slope which is often known as the rate of change and the other one is intercept which is basically an initial value. These models are very common in use when we are dealing with numeric data. Outcomes of these models can easily break down to reach over final results. Therefore, researchers, academicians, economists prefer these models.
This is a guide to Linear Model in R. Here we discuss the types, syntax, and parameters of the Linear Model in R along with its advantages. You can also go through our other suggested articles to learn more –
- What is Regression Analysis? | Working | Benefits
- An Introduction to Factors in R
- Poisson Regression in R
- Linear Regression Modeling
- Complete Guide to Multiple Linear Regression
- Guide to Multiple Linear Regression in R