2. 위의 X_train과 Y_train으로 선형 회귀모형 만들기
>>> model = lm(sales~TV, data=train) >>> summary(model) Call: lm(formula = sales ~ TV, data = train) Residuals: Min 1Q Median 3Q Max -8.3138 -1.9024 -0.1591 2.0736 7.2839 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.962685 0.548144 12.70 <2e-16 *** TV 0.047528 0.003222 14.75 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 3.294 on 138 degrees of freedom Multiple R-squared: 0.6119, Adjusted R-squared: 0.6091 F-statistic: 217.6 on 1 and 138 DF, p-value: < 2.2e-16
3. 위의 모형에 X_test로 예측값 계산하고 실제 Y_test와 비교해 MSE를 구하기
>>> Y_pred = predict(model, test) >>> mean((Y_pred-test$sales) ** 2) # MSE 10.10929