|
lm(formula = dependent variable ~ independent variable1 + independent variable2 + ..., data).
|
|
summary() - To get more details about the model results.
|
|
broom::glance() - See the model details in a tidier form.
|
|
predict(model, newdata)
|
|
ggplot(dframe, aes(x = pred, y = outcome)) + geom_point() + geom_abline()
|
|
GainCurvePlot(data, "prediction", "price", "model")
|
|
RMSE = sqrt(mean(res2)). It is the typical prediction error of your model on the data. Want to minimize this.
|
|
R2 = 1-(RSS/TSS). A measure of how well the model fits or explains the data.
|
|
Pros of Regression
|
|
Easy to fit and apply
|
|
Concise
|
|
Less prone to overfit
|
|
Interpretable - One can easily observe the signs and magnitude of each coefficient.
|
|
Things to look out for
|
|
Cannot express complex, non-linear, non-additive relationships. Non-linear relationships can be made linear by applying transformations.
|
|
Collinearity is when input variables are partially correlated. When independent variables are correlated, signs of the variables may not be what you expect. Moreover, it can be difficult to separate out the individual effects of collinear variables on the response.. Try manually removing offending variables, dimension reduction, or regularization.
|
|
Variance among residuals should be constant. Plot the residuals on the y-axis and predicted values on the x-axis. The errors should be evenly distributed between positive and negative, and have about the same magnitude above and below.
|
|
Autocorrelation - Errors are independent and uncorrelated. Plot the row_number() on the x-axis and residuals on the y-axis.
|
# Returns a list of nSplits.
# Each sub-list will contain two vectors; train and app.
splitPlan <- vtreat::kWayCrossValidation(nRows, nSplits, NULL, NULL)
# Initialize a column of the appropriate length
dframe$pred.cv <- 0
# k is the number of folds
# splitPlan is the cross validation plan
for(i in 1:k) {
# Get the ith split
split <- splitPlan[[i]]
# Build a model on the training data from this split (lm, in this case)
model <- lm(fmla, data = dframe[split$train,])
# make predictions on the application data from this split
dframe$pred.cv[split$app] <- predict(model, newdata = dframe[split$app,])
}
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.