Given data about sale of book as follow.
#To know current directory.
getwd()
## [1] "D:/BUKU UGI/Buku R Ugi dan Erwin/Memasang Kurva Pendekatan Kuadratik terhadap Data"
#Set to the current directory
setwd("D:/BUKU UGI/Buku R Ugi dan Erwin/Memasang Kurva Pendekatan Kuadratik terhadap Data")
#Read or open data "sale of book.csv" in R
data=read.csv("sale of book.csv")
#Show the data
data
## Year Sale
## 1 1 35
## 2 2 26
## 3 3 19
## 4 4 6
## 5 5 1
## 6 6 3
## 7 7 5
## 8 8 12
## 9 9 17
#Plot data in graphic
attach(data)
plot(Year, Sale, main="Scatterplot", xlab="Year ", ylab="Number of Book That Is Sold", pch=19)
#Fit a quadratic curve
fit2<-lm(Sale~poly(Year,2,raw=TRUE))
summary(fit2)
##
## Call:
## lm(formula = Sale ~ poly(Year, 2, raw = TRUE))
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.987 -1.872 -0.039 1.546 4.305
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 52.7857 3.3998 15.526 4.52e-06 ***
## poly(Year, 2, raw = TRUE)1 -17.1028 1.5611 -10.956 3.43e-05 ***
## poly(Year, 2, raw = TRUE)2 1.4686 0.1522 9.646 7.11e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.672 on 6 degrees of freedom
## Multiple R-squared: 0.9595, Adjusted R-squared: 0.946
## F-statistic: 71.07 on 2 and 6 DF, p-value: 6.645e-05
#Show coefficient of regression
fit2$coefficient[1]
## (Intercept)
## 52.78571
fit2$coefficient[2]
## poly(Year, 2, raw = TRUE)1
## -17.10281
fit2$coefficient[3]
## poly(Year, 2, raw = TRUE)2
## 1.468615
#Count value of sale estimation
quadratic = fit2$coefficient[3]*Year^2 + fit2$coefficient[2]*Year + fit2$coefficient[1]
quadratic
## [1] 37.151515 24.454545 14.694805 7.872294 3.987013 3.038961 5.028139
## [8] 9.954545 17.818182
#Fit quadratic curve
plot(Year, Sale, main="Scatterplot",
xlab="Year ", ylab="Number of Book That Is Sold ", pch=19)
par(new = TRUE)
lines(Year,quadratic, col="red")
Hopefully, this explanation is useful for us ^_^ ~Prana Ugi~