Programming Logic
Steps to fit the linear regression model for relationship between dependent and independent variables.
Step 1:
Find the correlation between dependent variable cpi and independent variables year and quarter
Step 2:
Scatter plot dependent vs independent variables
Step 3:
Fit the linear regression model and determine the coefficients
Step 3:
Using the model predict the cpi for each quarter of year 2015
Step 4:
Plot the cpi for previous years and for the predicted year
Plot to visualize the correlation
# cpi~ year + quarter
# plot cpi vs quarter to see the relationship
# plot cpi for each quarter for three years
# define x axis manually using axis function
plot(cpi, xaxt="n", ylab="CPI", xlab="")
axis(1, labels=paste(year, quarter, sep="-Q"), at=1:12, las=3)
Predict using linear regression model
# predict cpi for each quarter of year 2015
data2015 = data.frame(year=2015, quarter=1:4)
cpi2015 = predict(lrm, newdata = data2015)
cpi2015
# 1 2 3 4
# 174.7083 175.9417 177.1750 178.4083