शुरू करेंमुफ़्त में शुरू करें

टेस्ट डेटा पर soybean मॉडल से प्रिडिक्ट करें

इस अभ्यास में, आप पिछले अभ्यास के soybean मॉडलों (model.lin और model.gam, पहले से लोडेड) को नए डेटा soybean_test पर अप्लाई करेंगे.

यह अभ्यास पाठ्यक्रम का हिस्सा है

R में Supervised Learning: Regression

पाठ्यक्रम देखें

अभ्यास निर्देश

  • लीनियर मॉडल model.lin से प्रेडिक्शंस के साथ एक कॉलम soybean_test$pred.lin बनाएँ.
  • GAM मॉडल model.gam से प्रेडिक्शंस के साथ एक कॉलम soybean_test$pred.gam बनाएँ.
    • GAM मॉडलों के लिए, predict() मेथड एक मैट्रिक्स रिटर्न करता है, इसलिए मैट्रिक्स को वेक्टर में बदलने के लिए as.numeric() का उपयोग करें.
  • खाली स्थान भरकर प्रेडिक्शन कॉलम्स को pivot_longer() करके एक सिंगल वैल्यू कॉलम pred और की कॉलम modeltype में बदलें. लंबे फॉर्म वाला डेटा फ्रेम 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")
  
कोड संपादित करें और चलाएँ