Forecasting using Curve Fitting

with Random numbers

Synopsis

This paper is concerned with the use of Curve Fitting to forecast events.

We will use data from Random numbers. Data can come from various real life data and will be used later in this report.

library(Hmisc)
library(dplyr)
library(reshape2)
x  <- 0:10
summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0     2.5     5.0     5.0     7.5    10.0
sd(x)
## [1] 3.316625
#plot(x, type="o", col="blue", ylim=c(0,12))
y <- c(2,5,4,6,7,9,11,15,20,25,30)
summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.00    5.50    9.00   12.18   17.50   30.00
sd(y)
## [1] 9.217572
plot(y, type="o", col="blue", ylim=c(0,30))

First Order Equation

We will first attempt curve fitting using a First Order Equation (y = mx + b) a linear model

p <- plot(x,y,pch=19)
fit <- lm(y~x) 
print(p)
## NULL
lines(x,predict(fit,data.frame(x=x)),col="red")

Second Order Polynomial Equation

Same data only we plot using Second Order Polynomial Equation

p <- plot(x,y,pch=19)
fit <- lm(y~poly(x,2,raw=TRUE)) 
print(p)
## NULL
lines(x,predict(fit,data.frame(x=x)),col="red")

Third Order Polynomial Equation

We continue to plot , only now using Third Order Polynomial Equation

p <- plot(x,y,pch=19)
fit <- lm(y~poly(x,3,raw=TRUE)) 
print(p)
## NULL
lines(x,predict(fit,data.frame(x=x)),col="red")

Fourth Order Polynomial Equation

The curve appears to follow closely the data

p <- plot(x,y,pch=19)
fit <- lm(y~poly(x,4,raw=TRUE)) 
print(p)
## NULL
lines(x,predict(fit,data.frame(x=x)),col="red")

Least Squares Method for graphing

A very popular method is using The Least Squares Method to plot a graph

p <- plot(x,y,pch=19)
nlsFit <- nls(y ~b1*x^3-b2*x^2+b3*x+b4,start=list(b1 = 1,b2 = 3,b3 = 1,b4 = 1))
newdata <- data.frame(x = seq(min(x),max(x),len=100))
predictLine <- lines(newdata$x,predict(nlsFit,newdata=newdata),col="red")
print(predictLine)
## NULL
nlsFit1 <- nls(y ~ b1*x^3-b2*x^2+b3*x+b4,start=list(b1 = 1,b2 = 3,b3 = 1,b4 = 1))
predictLine1 <- lines(newdata$x,predict(nlsFit1,newdata=newdata),col="green")

print(predictLine1)
## NULL