首页 > 解决方案 > 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")

有人可以告诉我我做错了什么吗?

谢谢

标签: rstatisticsregressiondata-visualization

解决方案


您的代码中有几件事需要考虑;有些NaN值会导致不同的向量长度,并且您错误地传递newdatapredict

使用您的数据和模型:

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自动删除的值。它们是NaNwhen x = 0(看到 runy[x==0]z[x==0])由于sin(x)/x术语 being 0/0。所以这暗示了不同数量的观察可能来自哪里。

接下来你使用predict不正确。从?predict.gaussprnewdata应该是

包含新数据的数据框或矩阵

但是你传递了一个向量;实际上,您将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)) 作为其中一个yNaN,因此不会为该值生成预测。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在您的模型中一样,您还需要将一个或一些值传递ypredict语句中。您需要考虑使用什么值——也许是平均值?


一个稍微简单的工作流程是在开始建模之前准备数据,因为这可以更好地控制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") 

推荐阅读