【问题标题】:Why am I getting "Error: Problem with `mutate()` column `regression1`"?为什么我收到“错误:`mutate()` 列 `regression1` 有问题”?
【发布时间】:2021-12-25 06:28:12
【问题描述】:

我正在执行一项任务,我必须使用测试数据评估基于 RMSE(均方根误差)的预测模型。我已经建立了一个线性回归模型,使用基于训练数据的所有可用预测变量来预测葡萄酒质量(数字)。以下是我当前的代码。完整的错误是 “错误:mutate()regression1 有问题。 我regression1 = predict(regression1, newdata = my_type_test)。 x 没有适用于“c('double', 'numeric')”类对象的“预测”方法

install.packages("rsample")
library(rsample)

my_type_split <- initial_split(my_type, prop = 0.7)
my_type_train <- training(my_type_split)
my_type_test <- testing(my_type_split)  

my_type_train

regression1 <- lm(formula = quality ~ fixed.acidity + volatile.acidity + citric.acid + chlorides + free.sulfur.dioxide + total.sulfur.dioxide +
                  density + pH + sulphates + alcohol, data = my_type_train)

summary(regression1)
regression1

install.packages("caret")
library(caret)
install.packages("yardstick")
library(yardstick)
library(tidyverse)

my_type_test <- my_type_test %>% 
  mutate(regression1 = predict(regression1, newdata = my_type_test)) %>%
  
rmse(my_type_test, price, regression1)

【问题讨论】:

  • 你能提供数据或至少几行吗?没有数据很难复制这个问题。你可以使用dput()函数。
  • 你确定你的代码吗?结果变量名先是quality,然后好像是price?

标签: r linear-regression training-data predict


【解决方案1】:

您采取的许多步骤可能是不必要的。
应该实现相同目的的最小示例:

# Set seed for reproducibility
set.seed(42)
# Take the internal 'mtcars' dataset
data <- mtcars
# Get a random 80/20 split for the number of rows in data
split <- sample(
   ​size = nrow(data), 
   ​x = c(TRUE, FALSE), 
   ​replace = TRUE,
   ​prob = c(0.2, 0.8)
)
# Split the data into train and test sets
train <- data[split, ]
test <- data[!split, ]

# Train a linear model
fit <- lm(mpg ~ disp + hp + wt + qsec + am + gear, data = train)

# Predict mpg in test set
prediction <- predict(fit, test)

结果:

> caret::RMSE(prediction, test$mpg)
[1] 4.116142

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 2021-11-11
    • 1970-01-01
    • 2020-11-05
    • 2015-01-30
    • 2014-02-10
    • 2015-06-27
    相关资源
    最近更新 更多