线性回归预测:对类为“data.Frame”的对象应用“predict”方法时没有适用的方法

我有我的数据(目前是虚拟数据):

data_for_prediction <- original_data[,c(1,3)]

如果你想重现这个问题,以下内容就足够引发这个错误:

data_for_prediction <- data.frame(  diff = c(1,2,3),  f.mean_slope = c (1,2,3))

其中仅包含两列:“diff”(Y)和“f.mean_slope”(x)

然后我对整个数据集进行抽样:

set.seed(101)trainingRowIndex <- sample(1:nrow(data_for_prediction), 0.8*nrow(data_for_prediction))trainingData <- data_for_prediction[trainingRowIndex, ]  testData  <- data_for_prediction[-trainingRowIndex, ]

之后我创建了一个拟合模型:

model_fit <- lm(diff ~ ., data = trainingData, method = "model.frame")

当我尝试进行预测时:

newdata <- data.frame(  f.mean_slope = c(1,2,3))distPred <- predict(model_fit, newdata) 

R Studio 随后返回了错误消息:

错误在 UseMethod(“predict”):对类为“data.Frame”的对象应用“predict”方法时没有适用的方法

这让我很困扰,因为我在网上搜索了很多类似的问题,但都没有解决…

有谁有想法吗?


回答:

这是因为你使用了:

model_fit <- lm(diff ~ ., data = trainingData, method = "model.frame")class(model_fit)[1] "data.frame"

以上代码给你的是用来拟合数据的模型矩阵。

你可以这样做:

model_fit <- lm(diff ~ ., data = trainingData,model=TRUE)newdata <- data.frame(  f.mean_slope = c(1,2,3))distPred <- predict(model_fit, newdata) 

模型矩阵可以在 model_fit$model 中找到

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注