Load the data into R. The procedure for loading of data files has been shown in previous R tutorials. Here, we use a data set from a pre-installed package “fma”.
To load data from the package in R, use the commands: >data (package=”fma”) This instantly presents a list of all data sets available within the package. Now, to load the data set “advert”, use the command: >data(advert) Now print the data in R by: >advert
To fit the data into a simple linear regression model using R programming we use the following commands:
Define a variable name (here, model) and use the command: >model <- lm (sals~advert, data=advert) To view the intercept and slope of regression in R, print > model
To print the entire summary of the regression using R software, use the command: >summary (model)
The following command can be used to find the residuals of individual observations: >residuals (model)
To draw a scatter plot between sales and advert we use the following command: >plot (sales~advert, data= advert, main=” Sales and Advertising expenditure”, xlab=” Advertising expenditure”, ylab=” sales”)
main is used to define the title of the scatterplot while xlab and ylab are used to set X-axis and Y-axis labels.
Display line of best fit by using the following the command: >abline(model)
Forecast data when adverting expenditure is 30. Use the following command: >predict(model, newdata=data.frame(advert=30))