Predicting product sales using R language
through the different advertising inputs, the forecast Sales of products. Because the response variable sales is a sequential value, this problem is a regression problem. Data sets have a total of ten observations, and each group of observations corresponds to a market situation.
Data Features
- TV: For a single product in a given market, the cost of advertising on TV (in thousands)
- Radio: Advertising costs for investment in advertising media
- Newspaper: The cost of advertising for newspaper media
Response
- Sales: sale of corresponding products
Loading Data
> Data <-read.csv ("Http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv", Colclasses=c ("NULL", Na,na,na,na))>Head (data) TV Radio newspaper Sales1 230.1 37.8 69.2 22.12 44.5 39.3 45.1 10.43 17.2 45.9 69.3 9.34 151.5 41.3 58.5 18.55 180. 8 10.8 58.4 12.96 8.7 48.9 75.0 7.2#show the relationship between sales and TV> Plot (DATA$TV, Data$sales, col="Red", xlab='TV', ylab='Sales')
# fitting the relationship between Sales and TV ads with linear regression > fit=lm (sales~tv,data=data)# See the estimated coefficients > Coef (Fit) (Intercept) TV 7.03259355 0.04753664# shows the lines of the fitted model > abline (FIT)
# shows the relationship between Sales and Radio > Plot (Data$radio, data$sales, col="red", xlab=' Radio ', ylab='Sales')
# fitting the relationship between Sales and Radio ads with linear regression > fit1=lm (sales~radio,data=data)# view the estimated coefficients > Coef (fit1) (Intercept) Radio 9.3116381 0.2024958# shows the lines of the fitted model > Abline (fit1)
# show Sales and newspaper Relationships > Plot (data$newspaper, data$sales, col="red", xlab= 'Radio', ylab='Sales')
# fitting the relationship between Sales and radio ads with linear regression > fit2=lm (sales~newspaper,data=data)# See the estimated coefficients > Coef (fit2) (Intercept) newspaper 12.3514071 0.0546931# shows the lines of the fitted model > Abline (FIT2)
# Create scatter graph matrix > Pairs (~sales+tv+radio+newspaper,data=data, main="scatterplot Matrix ")
First line graphic display TV,Radio,newspaper impact on Sales . The longitudinal axis is Sales, the horizontal axes are TV,Radio,Newspaper . It can be seen thatTV features and sales are relatively strong linear relationship.
dividing training sets and test sets
> Trainrowcount <-Floor (0.8 * nrow (data))> Set.seed (1)> Trainindex <-sample (1: Nrow (data), Trainrowcount)> Train <- data[trainindex,]> Test <-data[-trainindex,] > Dim (data) [1] 4> Dim (Train) [1] 4> Dim (Test) [1] 4
Fit linear regression model
> Model <-LM (Sales~tv+radio+newspaper, Data=train)>Summary (model) CALL:LM (Formula= Sales ~ TV + Radio + newspaper, data =train) Residuals:min 1Q Median 3Q Max-8.7734-0.9290 0.2475 1.2213 2.8971coefficients:estimate Std. Error t value Pr (>|t|) (Intercept)2.840243 0.353175 8.042 2.07e-13 * * *TV0.046178 0.001579 29.248 < 2e-16 * * *Radio0.189668 0.009582 19.795 < 2e-16 * * *newspaper-0.001156 0.006587-0.176 0.861---signif. codes:0 '' 0.001 ' * * ' 0.01 ' * ' 0.05 '. ' 0.1 "1residual standard error:1.745 on 156degrees of Freedommultiple R-squared:0.8983, Adjusted r-squared:0.8963F-statistic:459.2 on 3 and156 DF, P-value: < 2.2e-16
predicting and calculating root-mean-square errors
> Predictions <- predict (model, test)> Mean ((test["Sales"]-predictions ) ^2) [1] 2.050666
Feature Selection
In the relationship between the previous variables and the sales volume, we see The linear relationship between newspaper and sales is weaker, and in the model abovethe coefficients of newspaper are negative, now remove this feature and look at the root mean square error of the results of linear regression predictions.
> Model1 <-lm (Sales~tv+radio, Data=train)>Summary (MODEL1) CALL:LM (Formula= Sales ~ TV + Radio, data =train) Residuals:min 1Q Median 3Q Max-8.7434-0.9121 0.2538 1.1900 2.9009coefficients:estimate Std. Error t value Pr (>|t|) (Intercept)2.821417 0.335455 8.411 2.35e-14 * * *TV0.046157 0.001569 29.412 < 2e-16 * * *Radio0.189132 0.009053 20.891 < 2e-16 * *---signif. codes:0 '' 0.001 ' * * ' 0.01 ' * ' 0.05 '. ' 0.1 "1residual standard error:1.74 on 157degrees of Freedommultiple R-squared:0.8983, Adjusted r-squared:0.897F-statistic:693 on 2 and157 DF, P-value: < 2.2e-16> predictions1 <-Predict (MODEL1, test)> Mean (test["Sales"]-predictions1) ^2)[1] 2.050226
from the above can be seen2.050226<2.050666and Willnewspaper This feature is removed, the resulting RMS error becomes smaller, indicating that newspaper is not suitable as a feature of forecast sales, then the newspaper is removed feature, a new model is obtained.
Predicting product sales using R language