| Start | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Linear regression on the mtcars dataset The built-in mtcars data frame (opens new window) contains information about 32 cars, including their weight, fuel efficiency (in miles-per-gallon), speed, etc. (To find out more about the dataset, use If we are interested in the relationship between fuel efficiency ( The plots shows a (linear) relationship!. Then if we want to perform linear regression to determine the coefficients of a linear model, we would use the The Which gives the output: This provides information about:
We could add a line to our first plot to show the predicted It is also possible to add the equation to that plot. First, get the coefficients with The result is:
|
||||||||||||||||||||||||||||||
| Weighting Sometimes we want the model to give more weight to some data points or examples than others. This is possible by specifying the weight for the input data while learning the model. There are generally two kinds of scenarios where we might use non-uniform weights over the examples:
The Test Data Analytic Weights Output Sampling Weights (IPW) Output |
||||||||||||||||||||||||||||||
| Using the 'predict' function Once a model is built If I had a new data source with displacement I could see the estimated miles per gallon. The most important part of the process is to create a new data frame with the same column names as the original data. In this case, the original data had a column labeled Caution Let's look at a few common pitfalls: Accuracy To check the accuracy of the prediction you will need the actual y values of the new data. In this example, |
||||||||||||||||||||||||||||||
| Checking for nonlinearity with polynomial regression Sometimes when working with linear regression we need to check for non-linearity in the data. One way to do this is to fit a polynomial model and check whether it fits the data better than a linear model. There are other reasons, such as theoretical, that indicate to fit a quadratic or higher order model because it is believed that the variables relationship is inherently polynomial in nature. Let's fit a quadratic model for the First we make a scatter plot of the variables A linear fit will show that Then, to get the result of a quadratic model, we added As we have three variables, the fitted model is a surface represented by:
Another way to specify polynomial regression is using Finally, what if we need to show a plot of the estimated surface? Well there are many options to make |
||||||||||||||||||||||||||||||
| Plotting The Regression (base) Continuing on the First fit the linear model and Then plot the two variables of interest and add the regression line within the definition domain: Almost there! The last step is to add to the plot, the regression equation, the rsquare as well as the correlation coefficient. This is done using the Note that you can add any other parameter such as the RMSE by adapting the vector function. Imagine you want a legend with 10 elements. The vector definition would be the following: and you will need to defined Here is the output: |
||||||||||||||||||||||||||||||
| Quality assessment After building a regression model it is important to check the result and decide if the model is appropriate and works well with the data at hand. This can be done by examining the residuals plot as well as other diagnostic plots. These plots check for two assumptions that were made while building the model:
Syntax
Parameters
|
Read Article
https://develop.horje.com/learn/1434/reference