r - R:`xy.coords(x,y)中的错误:'x'和'y'长度不同`
问题描述
我正在使用 R 编程语言。我正在尝试按照本教程中的说明创建回归模型并绘制结果(https://rdrr.io/cran/kernlab/man/gausspr.html):
#load library
library(kernlab)
# create regression data
x <- seq(-20,20,0.1)
y <- sin(x)/x + rnorm(401,sd=0.03)
# regression with gaussian processes
foo <- gausspr(x, y)
foo
# predict and plot
ytest <- predict(foo, x)
plot(x, y, type ="l")
lines(x, ytest, col="red")
#predict and variance
x = c(-4, -3, -2, -1, 0, 0.5, 1, 2)
y = c(-2, 0, -0.5,1, 2, 1, 0, -1)
plot(x,y)
foo2 <- gausspr(x, y, variance.model = TRUE)
xtest <- seq(-4,2,0.2)
lines(xtest, predict(foo2, xtest))
lines(xtest,
predict(foo2, xtest)+2*predict(foo2,xtest, type="sdeviation"),
col="red")
lines(xtest,
predict(foo2, xtest)-2*predict(foo2,xtest, type="sdeviation"),
col="red")
这很好用,但上面的代码是针对只有两个变量的回归问题。我正在尝试将此代码扩展为具有三个变量的回归问题。下面,我尝试为三个变量(x、y、z:响应变量为 z,预测变量为 x 和 y)重新创建上述代码:
# create regression data for new problem
x <- seq(-20,20,0.1)
y <- sin(x)/x + rnorm(401,sd=0.03)
z <- sin(x)/x + rnorm(401,sd=0.01)
#put into data frame
my_data = data.frame(x,y,z)
# regression with gaussian processes
foo <- gausspr(z ~., data = my_data)
foo
# predict and plot (this is where the error is)
ytest <- predict(foo, c(x,y))
#plot
plot(x, y, type ="l")
lines(x, ytest, col="red")
这会产生以下错误:Error in xy.coords(x, y) : 'x' and 'y' lengths differ
是否有另一种方法可以指定您希望使用“x”和“y”变量进行预测?我想在 R 中,您可以将c
命令用于这样的实例吗?
ytest <- predict(foo, c(x,y))
这使我无法继续前进并在高斯过程 (foo2) 与 xtest 和 ytest 之间制作两个单独的图,其中显示了置信区间:
foo2 <- gausspr(z ~., data = my_data, variance.model = TRUE)
xtest <- seq(-4,2,0.2)
ytest <- seq(-4,2,0.2)
#first plot
lines(xtest, predict(foo2, xtest))
lines(xtest,
predict(foo2, xtest)+2*predict(foo2,xtest, type="sdeviation"),
col="red")
lines(xtest,
predict(foo2, xtest)-2*predict(foo2,xtest, type="sdeviation"),
col="red")
#second plot
lines(ytest, predict(foo2, ytest))
lines(ytest,
predict(foo2, ytest)+2*predict(foo2,ytest, type="sdeviation"),
col="red")
lines(ytest,
predict(foo2, ytest)-2*predict(foo2,ytest, type="sdeviation"),
col="red")
有人可以告诉我我做错了什么吗?
谢谢
解决方案
您的代码中有几件事需要考虑;有些NaN
值会导致不同的向量长度,并且您错误地传递newdata
了predict
。
使用您的数据和模型:
library(kernlab)
x <- seq(-20,20,0.1)
y <- sin(x)/x + rnorm(401,sd=0.03)
z <- sin(x)/x + rnorm(401,sd=0.01)
my_data <- data.frame(x,y,z)
foo <- gausspr(z ~., data = my_data)
请注意,在此阶段使用了400 个数据点,gausspr
而不是 401 个。
foo
... 学习的训练实例数:400
这是由于y
并且z
具有NaN
自动删除的值。它们是NaN
when x = 0
(看到 runy[x==0]
和z[x==0]
)由于sin(x)/x
术语 being 0/0
。所以这暗示了不同数量的观察可能来自哪里。
接下来你使用predict
不正确。从?predict.gausspr
newdata应该是
包含新数据的数据框或矩阵
但是你传递了一个向量;实际上,您将x
和连接y
到一个向量中c(x,y)
。所以改变
ytest <- predict(foo, c(x,y))
到
ytest <- predict(foo, data.frame(x=x, y=y)) # or cbind(x,y)
请注意,有 400 个样本内预测 ( length(ytest)
) 作为其中一个y
值NaN
,因此不会为该值生成预测。To plot
, 和 的长度x
必须y
相同,因此x=0
必须删除与麻烦项相关的值。
plot(x, y, type ="l") # x and y are both length 401
lines(x[x != 0], ytest, col="red") # both length 400
您问题的下一段代码中还有几个错误。
如果只有一个预测因子,那么
predict(foo2, xtest)
应该
predict(foo2, data.frame(x=xtest))
但是,就像y
在您的模型中一样,您还需要将一个或一些值传递y
到predict
语句中。您需要考虑使用什么值——也许是平均值?
一个稍微简单的工作流程是在开始建模之前准备数据,因为这可以更好地控制NA
数据NAN
的处理方式。例如
# remove NA and NaN
my_data <- data.frame(x,y,z)
model_data <- na.omit(my_data)
# run model and predict
foo <- gausspr(z ~., data = model_data)
model_data$ytest <- predict(foo, data.frame(x=x, y=y))
# plot
plot(y ~ x, data=model_data, type ="l")
lines(ytest ~ x, data=model_data, col="red")
推荐阅读
- python - 使用 SQL Alchemy 表示通用决策树
- php - feof() 和 fread() 产生错误
- c# - 在 ASP.NET Core 中验证后显示用户信息
- c# - 将列表从 c# 发送到 Oracle 进行批处理
- javascript - 谷歌地图 api ugrade php
- php - 无法发送邮件。邮件程序错误:
- java - 将测试结果写入 CSV 文件
- swift - 如何从 JSON 中过滤多个值并将结果附加到 TableView Swift4
- python - 如何将字符串拆分为具有给定长度但不破坏句子的子字符串?
- redirect - 在 NGINX 脚本中处理重定向字符串,可以吗?