상대 오차
이번 연습에서는 상대 오차를 절대 오차와 비교해 보겠습니다. 모델링 목적에서 상대 오차를 다음과 같이 정의하겠습니다.
$$ rel = \frac{(y - pred)}{y} $$
즉, 오차를 실제 결과값에 대해 상대적으로 본다는 뜻입니다. 모델의 전체 상대 오차는 root mean squared relative error로 측정합니다:
$$ rmse_{rel} = \sqrt(\overline{rel^2}) $$
여기서 $\overline{rel^2}$는 $rel^2$의 평균입니다.
예시(장난감) 데이터셋 fdata가 미리 로드되어 있습니다. 다음 열을 포함합니다:
y: 어떤 모델이 예측해야 하는 실제 출력값입니다. 고객이 매장 방문 한 번에 지출하는 금액이라고 가정해 보세요.pred:y를 예측한 모델의 예측값입니다.label: 범주형으로,y가small(소액) 구매 집단에서 왔는지,large(고액) 구매 집단에서 왔는지를 나타냅니다.
어떤 모델이 더 “좋은지”, 즉 small 구매를 예측하는 모델과 large 구매를 예측하는 모델 중 어느 쪽이 더 나은지 알고 싶습니다.
이 연습은 강의의 일부입니다
R로 하는 Supervised Learning: 회귀
연습 안내
- 빈칸을 채워 데이터를 살펴보세요. 고액 구매는 소액 구매보다 대략 100배 정도 큰 경향이 있음을 확인해 보세요.
- 빈칸을 채워 오차 열을 만드세요:
- 잔차는
y - pred로 정의하세요. - 상대 오차는
residual / y로 정의하세요.
- 잔차는
- 빈칸을 채워 RMSE와 상대 RMSE를 계산하고 비교하세요.
- 절대 오차는 어떻게 비교되나요? 상대 오차는 어떤가요?
- 예측값과 실제값의 산점도를 살펴보세요.
- 여러분의 판단으로, 어느 모델이 더 “좋은가요?”
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# fdata is available
summary(fdata)
# Examine the data: generate the summaries for the groups large and small:
fdata %>%
group_by(label) %>% # group by small/large purchases
summarize(min = ___, # min of y
mean = ___, # mean of y
max = ___) # max of y
# Fill in the blanks to add error columns
fdata2 <- fdata %>%
group_by(label) %>% # group by label
mutate(residual = ___, # Residual
relerr = ___) # Relative error
# Compare the rmse and rmse.rel of the large and small groups:
fdata2 %>%
group_by(label) %>%
summarize(rmse = ___, # RMSE
rmse.rel = ___) # Root mean squared relative error
# Plot the predictions for both groups of purchases
ggplot(fdata2, aes(x = pred, y = y, color = label)) +
geom_point() +
geom_abline() +
facet_wrap(~ label, ncol = 1, scales = "free") +
ggtitle("Outcome vs prediction")