|
Clever Algorithms Welcome to CleverAlgorithms.com! |
Get notified of future announcements!
|
Please Note that this is an early access preview of the book.
Get notified when the book is released!
Ordinary Least Squares RegressionOrdinary Least Squares Regression, Ordinary Linear Regression, OLS, OLSR, Linear Least Squares TaxonomyOrdinary Least Squares is a method for estimating the parameters for a regression model. It is related to other least squares methods for estimating the parameters of linear models such as Weighted Least Squares and Partial Least Squares. StrategyThe information processing objective of the Ordinary Least Squares Regression is to define a line a best fit. The line is defined by an equation that minimizes the Sum of the Squared Residuals (SSR) with an intercept and a regression coefficient. Heuristics
Code ListingListing (below) provides a code listing of the Ordinary Least Squares method in R. Figure (below) provides a plot of the training dataset with the line of best fit highlighted.
The example uses the
The test problem is a two-dimensional dataset of 100 samples, where the # define 2D regression problem of 100 samples
regression_dataset <- function() {
x <- runif(100, 0, 10)
y <- x + rnorm(100)
data.frame(x, y)
}
# get the data
data <- regression_dataset()
# split data in to train and test (67%/33%)
training_set <- sample(100,67)
train <- data[training_set,]
test <- data[(1:100)[-training_set],]
# create a linear regression model using ordinary least squares
model <- lm(
y~x, # predict Y given X
train, # training dataset
NULL, # no weighting on the variables
NULL, # no action on missing values
method="qr") # QR decomposition (efficient matrix calculation method)
# summarize the create linear regression model
summary(model)
# regression model diagnostic plots with the training data
par(mfrow=c(2,2))
plot(model)
# plot the training data and the line of best fit
plot(train)
abline(model$coef, lty=5, col="red")
# make predictions for the test data
predictions <- predict.lm(model, test[,1:2])
# compute mean squared error
print(mean((test$y-predictions)^2))
Example of Ordinary Least Squares in R using the
lm function in the stats core package.Download: stats_ordinary_least_squares.R. Unit test available in the github project
Plot 2D training dataset with the line of best fit.
ReferencesPrimary SourcesThe method for Least Squares may be traced back to Gauss who claims to have devised the method in and later published it in 1809 [Gauss1809] [Gauss1823] (Latin). Legendre independently developed and published the Least Squares method in 1805 [Legendre1805] (Appendix, French). More InformationLeast Squares Regression is covered in any good text of statistics, data analysis or econometrics. Some popular introductory texts to least squares regression include Montgomery et al. [Montgomery2001] (Chapter 2), and Tamhane and Dunlop [Tamhane2000] (Chapter 10). Faraway provides a free book that includes a practical introduction into Least Squares Regression with examples in R [Faraway2002] (updated version published [Faraway2004]). Bibliography
|
Buy the paperback!Coming soon... Grab the PDFComing soon... ContributeMore in the SeriesCheck-out other books in the series.
|
||||||||||||||||||||
|
Please Note: This content was automatically generated from the book content and may contain minor differences. |