시작하기무료로 시작하기

테스트 데이터에 콩 모델로 예측하기

이 연습 문제에서는 이전 연습에서 만든 콩 모델(model.linmodel.gam, 이미 로드됨)을 새 데이터 soybean_test에 적용해 보겠습니다.

이 연습은 강의의 일부입니다

R로 하는 Supervised Learning: 회귀

강의 보기

연습 안내

  • 선형 모델 model.lin의 예측으로 열 soybean_test$pred.lin을 만드세요.
  • GAM 모델 model.gam의 예측으로 열 soybean_test$pred.gam을 만드세요.
    • GAM 모델의 경우 predict() 메서드는 행렬을 반환하므로, as.numeric()으로 벡터로 변환하세요.
  • 공백을 채워 예측 열들을 하나의 값 열 pred와 키 열 modeltype으로 pivot_longer() 하세요. 길게 변환한 데이터 프레임의 이름은 soybean_long으로 하세요.
  • 두 모델의 RMSE를 계산해 비교하세요.
    • 어떤 모델이 더 낫나요?
  • 각 모델의 예측을 실제 평균 잎 무게와 비교하기 위해 코드를 실행하세요.
    • Time에 따른 weight의 산점도.
    • Time에 따른 예측값(pred)의 점-선 그래프.
    • 선형 모델은 때때로 음수 무게를 예측해요! gam 모델도 그런가요?

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# soybean_test is available
summary(soybean_test)

# Get predictions from linear model
soybean_test$pred.lin <- ___(___, newdata = ___)

# Get predictions from gam model
soybean_test$pred.gam <- ___(___(___, newdata = ___))

# Pivot the predictions into a "long" dataset
soybean_long <- soybean_test %>%
  pivot_longer(cols = c(___, ___), names_to = ___, values_to = ___)

# Calculate the rmse
soybean_long %>%
  mutate(residual = weight - pred) %>%     # residuals
  group_by(modeltype) %>%                  # group by modeltype
  summarize(rmse = ___(___(___))) # calculate the RMSE

# Compare the predictions against actual weights on the test data
soybean_long %>%
  ggplot(aes(x = Time)) +                          # the column for the x axis
  geom_point(aes(y = weight)) +                    # the y-column for the scatterplot
  geom_point(aes(y = pred, color = modeltype)) +   # the y-column for the point-and-line plot
  geom_line(aes(y = pred, color = modeltype, linetype = modeltype)) + # the y-column for the point-and-line plot
  scale_color_brewer(palette = "Dark2")
  
코드 편집 및 실행