로그 변환한 금액형 출력 변수 모델링
이 연습 문제에서는 금액형 출력 변수를 로그로 변환해 모델링한 뒤, "log-money" 예측값을 다시 금액 단위로 되돌리는 과정을 연습해 보겠습니다. 로드된 데이터에는 2005년 소득(Income2005)과, 1981년에 실시한 여러 적성 검사 결과가 포함되어 있습니다:
ArithWordParagMathAFQT(Armed Forces Qualifying Test의 백분위)
데이터는 이미 학습 세트와 테스트 세트(income_train, income_test)로 분할되어 미리 로드되어 있습니다. 입력 변수들로부터 log(income)을 모델링한 다음, log(income)을 다시 income으로 변환해 보세요.
이 연습은 강의의 일부입니다
R로 하는 Supervised Learning: 회귀
연습 안내
- 학습 세트의 소득 요약 통계를 확인하려면
income_train$Income2005에summary()를 호출하세요. - 다섯 가지 검사 점수를 사용해
log(Income2005)를 설명하는 공식을 변수fmla.log에 작성하고, 출력하세요. income_train데이터에 대해log(Income2005)의 선형 모델model.log를 적합하세요.model.log를 사용해income_test데이터셋에서 소득을 예측하고, 결과를logpred열에 넣으세요.logpred의summary()를 확인해 보면 값의 크기가Income2005와 많이 다름을 볼 수 있습니다.
- 로그 변환을 반대로 적용해 예측값을 "금액 단위"로 되돌리세요:
exp(income_test$logpred).pred.income의summary()를 확인해 보면 이제 값의 크기가Income2005와 유사해졌음을 볼 수 있습니다.
- 테스트 세트에서 예측 소득과 실제 소득의 산점도를 그리도록 빈칸을 채우세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Examine Income2005 in the training set
___
# Write the formula for log income as a function of the tests and print it
(fmla.log <- ___)
# Fit the linear model
model.log <- ___
# Make predictions on income_test
income_test$logpred <- ___
summary(income_test$logpred)
# Convert the predictions to monetary units
income_test$pred.income <- ___
summary(income_test$pred.income)
# Plot predicted income (x axis) vs income
ggplot(___, aes(x = ___, y = ___)) +
geom_point() +
geom_abline(color = "blue")