#any packages used put here
library(readxl)
library(Metrics)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
hwdata<- read_excel('/Users/dylansnelsire/Desktop/301 Data.xlsx')
hwdata$size <- as.double(hwdata$size)
## Warning: NAs introduced by coercion
hwdata
## # A tibble: 99 × 4
## School size average_viewers NIL
## <chr> <dbl> <dbl> <dbl>
## 1 Alabama 38316 5110000 217000
## 2 Georgia 40118 3500000 139000
## 3 Texas 51991 3060000 291000
## 4 Ohio State 61677 5800000 125000
## 5 LSU 35912 3220000 98000
## 6 Miami 19096 608000 100000
## 7 Oklahoma 28042 1748000 122000
## 8 Oregon 22257 2210000 76000
## 9 Notre Dame 13139 3300000 83000
## 10 Clemson 27341 2590000 89000
## # … with 89 more rows
# Display the head of the data
head(hwdata)
## # A tibble: 6 × 4
## School size average_viewers NIL
## <chr> <dbl> <dbl> <dbl>
## 1 Alabama 38316 5110000 217000
## 2 Georgia 40118 3500000 139000
## 3 Texas 51991 3060000 291000
## 4 Ohio State 61677 5800000 125000
## 5 LSU 35912 3220000 98000
## 6 Miami 19096 608000 100000
ggplot(hwdata, aes(x = size, y = average_viewers)) +
geom_point() +
stat_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).
cor(hwdata$average_viewers, hwdata$size)
## [1] NA
# Perform simple linear regression
model <- lm(size ~ average_viewers, data = hwdata)
model
##
## Call:
## lm(formula = size ~ average_viewers, data = hwdata)
##
## Coefficients:
## (Intercept) average_viewers
## 2.673e+04 5.247e-03
#splitting the data
## 75% of the sample size
smp_size <- floor(0.80 * nrow(hwdata))
## set the seed to make your partition reproducible
set.seed(123)
train_ind <- sample(seq_len(nrow(hwdata)), size = smp_size)
train <- hwdata[train_ind, ]
test <- hwdata[-train_ind, ]
#multiple linear regreassion model
names(train)
## [1] "School" "size" "average_viewers" "NIL"
model <- lm(average_viewers ~ size + NIL, data = train)
summary(model)
##
## Call:
## lm(formula = average_viewers ~ size + NIL, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2178991 -367891 -244053 246211 3203831
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.083e+04 2.123e+05 0.098 0.9221
## size 1.276e+01 6.204e+00 2.057 0.0432 *
## NIL 1.431e+01 2.382e+00 6.006 6.33e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 872200 on 75 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.4244, Adjusted R-squared: 0.409
## F-statistic: 27.65 on 2 and 75 DF, p-value: 1.012e-09
#predicting r and rse
print("predict function")
## [1] "predict function"
predict(model, hwdata = size, se.fit=TRUE, interval="confidence")
## $fit
## fit lwr upr
## 1 909358.5 711143.07 1107573.9
## 2 637353.8 417758.46 856949.2
## 3 859291.6 654065.69 1064517.4
## 4 2228441.7 1534384.67 2922498.7
## 5 465000.7 164478.91 765522.5
## 6 575489.8 317715.51 833264.1
## 7 789399.8 578371.69 1000427.9
## 8 598527.6 370247.26 826807.8
## 9 336381.3 41222.15 631540.4
## 10 1178386.0 900906.69 1455865.3
## 11 579373.3 352318.96 806427.7
## 12 899480.9 547733.19 1251228.6
## 13 1375907.7 966769.34 1785046.1
## 14 639815.0 407026.69 872603.4
## 15 967380.0 731635.23 1203124.8
## 16 2124026.8 1670047.69 2578006.0
## 17 385281.9 97972.84 672591.0
## 18 400778.1 120067.33 681488.9
## 19 807811.2 602483.75 1013138.7
## 20 605931.3 377193.29 834669.2
## 21 370501.3 52005.44 688997.1
## 22 712943.9 475569.76 950318.0
## 23 2605912.1 2061260.34 3150563.9
## 24 1853959.5 1549554.99 2158364.1
## 25 459791.8 181890.02 737693.7
## 26 386694.5 87716.52 685672.5
## 27 768372.9 529044.74 1007701.0
## 28 980085.9 780064.67 1180107.0
## 29 1147748.9 815051.10 1480446.7
## 30 451371.4 174671.18 728071.6
## 31 1078799.9 796631.45 1360968.4
## 32 587932.9 346848.95 829016.9
## 33 379129.2 65561.21 692697.3
## 34 607211.0 384391.22 830030.8
## 35 731547.0 521042.35 942051.6
## 36 349907.6 56051.78 643763.4
## 37 584472.2 344408.67 824535.7
## 38 804702.0 589550.57 1019853.5
## 39 1881111.1 1552316.13 2209906.0
## 40 1392121.9 1081252.72 1702991.1
## 41 2199332.4 1753017.91 2645646.9
## 42 2724575.5 2214182.16 3234968.8
## 43 1334827.3 1041659.46 1627995.2
## 44 1233824.4 1019871.29 1447777.6
## 45 823685.9 530876.71 1116495.1
## 46 508484.5 266339.52 750629.5
## 47 573000.7 345736.52 800264.8
## 48 1047184.4 848201.44 1246167.5
## 49 899800.7 560997.95 1238603.4
## 50 837319.0 539327.69 1135310.2
## 51 949853.5 649867.33 1249839.7
## 52 415291.7 140419.55 690163.9
## 53 2390990.6 1890304.24 2891677.0
## 54 1279043.6 1066012.22 1492075.0
## 55 882404.5 682418.54 1082390.4
## 56 1695130.3 1274853.99 2115406.7
## 57 259186.9 -133591.43 651965.3
## 58 440430.3 152482.31 728378.4
## 60 405357.9 139006.38 671709.4
## 61 1418787.1 897683.78 1939890.5
## 62 1558320.8 954569.01 2162072.6
## 63 1152753.0 948435.24 1357070.9
## 64 878825.2 633014.91 1124635.5
## 65 2596169.4 2111098.80 3081240.0
## 66 468858.9 205736.06 731981.8
## 67 787964.3 493055.13 1082873.5
## 68 1072246.5 678916.83 1465576.2
## 69 436861.5 176565.71 697157.3
## 70 630969.8 322970.24 938969.3
## 71 392515.4 60958.96 724071.8
## 72 1426499.7 1175309.74 1677689.7
## 73 702433.9 457240.49 947627.2
## 74 594796.7 365175.72 824417.7
## 75 573792.5 346268.20 801316.7
## 76 380177.4 89908.83 670446.0
## 77 4847378.6 3699151.15 5995606.1
## 78 504549.4 258603.16 750495.6
## 79 989641.5 634755.65 1344527.4
##
## $se.fit
## [1] 99500.63 110232.98 103019.76 348404.32 150856.62 129398.13 105932.37
## [8] 114592.66 148164.65 139289.70 113977.28 176571.12 205380.23 116855.63
## [15] 118339.71 227889.49 144224.07 140911.84 103070.75 114822.41 159879.27
## [22] 119157.60 273405.55 152805.69 139501.79 150081.67 120138.48 100407.09
## [29] 167008.40 138898.59 141643.58 121019.88 157405.59 111851.59 105669.60
## [36] 147510.41 120507.64 108002.23 165049.24 156050.83 224041.98 256208.42
## [43] 147165.08 107400.69 146985.03 121552.49 114082.58 99885.95 170072.98
## [50] 149586.34 150587.74 137980.95 251335.70 106937.98 100389.39 210971.28
## [57] 197167.79 144544.81 133703.73 261584.65 303072.70 102563.92 123392.42
## [64] 243496.86 132083.01 148039.19 197444.55 130663.87 154610.32 166435.44
## [71] 126092.92 123082.72 115265.66 114213.15 145709.69 576389.85 123460.65
## [78] 178146.42
##
## $df
## [1] 75
##
## $residual.scale
## [1] 872223.5
Interpration: The model does not seem to be overfitting or undercutting, the distribution of the data is not showing the model working abnormally well or poor. The model takes longer to predict with the test data than on the training data and with the test data it performs well and the fit is very evenly distributed.