This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
# Importing the data
XUVData = as.data.frame(XUV_company_annual_profit)
head(XUVData)
NA
str(XUVData)
'data.frame': 13 obs. of 2 variables:
$ Year : num 2006 2007 2008 2009 2010 ...
$ Annual Profit (in Crores): num 88 92 95 95 90 85 98 95 108 118 ...
# assigning the data
t = seq(2006,2018,1)
t
[1] 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
#computing n
n = length(xt)
n
[1] 13
#shifting the origin and scale
xt = (t-2012)/1
xt
[1] -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6
#Assigning profit data
yt=XUVData$`Annual Profit (in Crores)`
yt
[1] 88 92 95 95 90 85 98 95 108 118 119 117 130
#Fitting linear trend to the data
linear_model<-lm(formula = yt~xt)
linear_model
Call:
lm(formula = yt ~ xt)
Coefficients:
(Intercept) xt
102.308 3.231
#summary of the fitting linear model
summary(linear_model)
Call:
lm(formula = yt ~ xt)
Residuals:
Min 1Q Median 3Q Max
-14.077 -4.308 2.385 5.615 8.308
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 102.3077 2.0158 50.753 2.14e-14 ***
xt 3.2308 0.5387 5.997 8.97e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 7.268 on 11 degrees of freedom
Multiple R-squared: 0.7658, Adjusted R-squared: 0.7445
F-statistic: 35.96 on 1 and 11 DF, p-value: 8.967e-05
#Extracting the estimates for linear trend
lm(yt~xt)[['coefficients']]
(Intercept) xt
102.307692 3.230769
#computing the fitted values for linear trend
fittedlinyt=lm(yt~xt)[['fitted.values']]
fittedlinyt
1 2 3 4 5 6 7 8 9
82.92308 86.15385 89.38462 92.61538 95.84615 99.07692 102.30769 105.53846 108.76923
10 11 12 13
112.00000 115.23077 118.46154 121.69231
#Fitting quadratic trend to the data
quadratic_model<-lm(yt~xt+I(xt^2))
quadratic_model
Call:
lm(formula = yt ~ xt + I(xt^2))
Coefficients:
(Intercept) xt I(xt^2)
96.9510 3.2308 0.3826
#summary of the fitting linear model
summary(quadratic_model)
Call:
lm(formula = yt ~ xt + I(xt^2))
Residuals:
Min 1Q Median 3Q Max
-9.103 -3.341 1.049 3.057 7.913
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 96.9510 2.2438 43.208 1.06e-12 ***
xt 3.2308 0.3978 8.122 1.03e-05 ***
I(xt^2) 0.3826 0.1199 3.190 0.00965 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 5.366 on 10 degrees of freedom
Multiple R-squared: 0.8839, Adjusted R-squared: 0.8607
F-statistic: 38.07 on 2 and 10 DF, p-value: 2.108e-05
#Extracting the estimates for quadratic trend
lm(yt~xt+I(xt^2))[['coefficients']]
(Intercept) xt I(xt^2)
96.9510490 3.2307692 0.3826174
#computing the fitted values for quadratic trend
fittedquadyt=lm(yt~xt+I(xt^2))[['fitted.values']]
fittedquadyt
1 2 3 4 5 6 7 8 9
91.34066 90.36264 90.14985 90.70230 92.01998 94.10290 96.95105 100.56444 104.94306
10 11 12 13
110.08691 115.99600 122.67033 130.10989
#appropriate trend for this data.
#The appropriate trend for this data is quadratic trends because its fitting trend has shown a high value of Adjusted R-squared than linear trend which indicates the best fit.
#plotting the actual values with the fitted values
plot(t,yt,type='l', lty=1, col='black',xlim=c(2006,2018),
lwd=2,main = 'Fitting ofLinear and quadratic trend')
lines(t,fittedlinyt, type = 'l',lty=2, col='blue', lwd=2)
lines(t,fittedquadyt, type = 'l',lty=2, col='red', lwd=2)
legend('topleft', lty=c(1,2),
col = c('black','blue','red'),c('original','Linear fitted','Quadratic fitted'))
#The fitted values corresponding to the appropriate trend onlyfittedquadyt=lm(yt~xt+I(xt^2))[['fitted.values']]
fittedquadyt
1 2 3 4 5 6 7 8 9
91.34066 90.36264 90.14985 90.70230 92.01998 94.10290 96.95105 100.56444 104.94306
10 11 12 13
110.08691 115.99600 122.67033 130.10989
#changing the origin and scale 2019
Quad_projxt = (2019-2012)/1
Quad_projxt
[1] 7
# Computing projected profit for year 2019
projected_profit= 96.9510490+(3.2307692*Quad_projxt)+(0.3826174*Quad_projxt^2)
projected_profit
[1] 138.3147
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.