(R) Xi - Xj 中的错误:二元运算符的非数字参数 [英] (R) Error in Xi - Xj : non-numeric argument to binary operator

查看:53
本文介绍了(R) Xi - Xj 中的错误:二元运算符的非数字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R 编程语言.我正在尝试重新创建本教程中显示的图表:

#PART2#创建新数据obs <- data.frame(x = c(-4, -3, -1, 0, 2),y = c(-2, 0, 1, 2, -1))#重复第 1 部分的步骤cov_xx_inv <-解决(cov(obs$x, obs$x))Ef <- cov(x_predict, obs$x) %*% cov_xx_inv %*% obs$yCf <- cov(x_predict, x_predict) - cov(x_predict, obs$x) %*% cov_xx_inv %*% cov(obs$x, x_predict)值 <- mvrnorm(200, Ef, Cf)数据 <- data.frame(x=x_predict, t(values)) %>%tidyr::pivot_longer(-x, names_to = "rep", values_to = "value") %>%变异(rep = as.numeric(as.factor(rep)))gp <- data.frame(x = x_predict, Ef = Ef, sigma = 2*sqrt(diag(Cf)))ggplot(dat,aes(x=x,y=value)) +geom_line(aes(group=rep), color = rgb(0.7, 0.1, 0.4), alpha = 0.2) + #REPLICATESgeom_ribbon(数据= gp,aes(x,y = Ef,ymin = Ef - 西格玛,ymax = Ef + sigma),填充=灰色",alpha = 0.4) +geom_line(dat = gp, aes(x=x,y=Ef), size=1) + #MEANgeom_point(data=obs,aes(x=x,y=y)) + #观察数据scale_y_continuous(lim=c(-3,3), name=输出,f(x)") +xlab(输入,x")

现在,我正在尝试为具有 3 个变量(1 个响应,2 个预测变量)的回归模型复制上述教程.我试图制作x_predict"对象有两列:

x_predict_1 <- seq(-5,5,len=50)x_predict_2 <- seq(-6,6,len=50)l <- 1x_predict <- data.frame(x_predict_1, x_predict_2)COV <- cov(x_predict, x_predict)

但这会产生以下错误:

Xi - Xj 中的错误:二元运算符的非数字参数

此错误阻止我创建值"和数据"第 1 部分中的对象,我无法创建所需的图形(例如 x_predict_1 与值和 x_predict_2 与值).这也妨碍了我在第 2 部分中创建所需的图表.

有人可以告诉我如何解决这个问题吗?谢谢

解决方案

我想我遇到了问题.首先,下面是我们可以重现错误的方式您进行的方式:

#PART 1#加载库图书馆(海量)图书馆(tidyverse)#设置种子set.seed(12345)#创建初始数据x_predict <- seq(-5,5,len=50)l <- 1#define 函数用于评估协方差SE <- 函数(Xi,Xj, l) exp(-0.5 * (Xi - Xj) ^ 2/l ^ 2)cov <- 函数(X,Y)外(X,Y,SE,l)COV <- cov(x_predict, x_predict)#对这些函数进行采样,将它们放入数据框中并绘制值 <- mvrnorm(200, rep(0, length=length(x_predict)), COV)数据 <- data.frame(x=x_predict, t(values)) %>%tidyr::pivot_longer(-x, names_to = "rep", values_to = "value") %>%变异(rep = as.numeric(as.factor(rep)))ggplot(dat,aes(x=x,y=value)) +geom_line(aes(group=rep), color = rgb(0.7, 0.1, 0.4), alpha = 0.4)x_predict_1 <- seq(-5,5,len=50)x_predict_2 <- seq(-6,6,len=50)l <- 1x_predict <- data.frame(x_predict_1, x_predict_2)COV <- cov(x_predict, x_predict)

在这段代码的最后会出现一个错误,同样由 Noob 突出显示

这里需要注意的是,R中存在的cov函数被重新定义并设置如下

cov <- function(X, Y) 外层(X, Y, SE, l)

此自定义函数仅适用于向量/数组,而不适用于 data.frames,后者是用于连接 x_predict_1 & 的方法.x_predict_1 同时扩展代码.

如果这个自定义函数在 data.frame 对象上调用,它总是会导致错误,因为它不是为了处理 data.frame 而构建的,它只是为数字向量构建的 &数组

现在,当任何新人尝试从两者之间复制它时,默认情况下,他将使用基础 R. 中的cov"函数,该函数适用于 data.frame 对象.因此强烈建议不要在 R 中重新定义现有函数,这会导致很多混乱.如果我们删除自定义的 'cov' 函数并调用 cov(x_predict, x_predict) 它将正常工作,而该函数将从基础 R 包中调用.

所以要解决这个问题,Noob 你只需要在加入 x_predict_1 & 时使用 'c'(组合)而不是 'data.frame'x_predict_2,您的问题将得到解决.我给出了我用你的变量试过的完整代码:

库(MASS)图书馆(tidyverse)#设置种子set.seed(12345)SE <- 函数(Xi,Xj, l) exp(-0.5 * (Xi - Xj) ^ 2/l ^ 2)cov <- 函数(X,Y)外(X,Y,SE,l)x_predict_1 <- seq(-5,5,len=50)x_predict_2 <- seq(-6,6,len=50)l <- 1x_predict <- c(x_predict_1, x_predict_2)头(x_predict,5)COV <- cov(x_predict, x_predict)值 <- mvrnorm(200, rep(0, length=length(x_predict)), COV)数据 <- data.frame(x=x_predict, t(values)) %>%tidyr::pivot_longer(-x, names_to = "rep", values_to = "value") %>%变异(rep = as.numeric(as.factor(rep)))ggplot(dat,aes(x=x,y=value)) +geom_line(aes(group=rep), color = rgb(0.7, 0.1, 0.4), alpha = 0.4)

最终结果将在图表下方.我希望这个解释能解决你的问题,如果没有,请告诉我.

在这里,如果您不想使用 'c'(组合),您可以使用 cbind 并创建一个矩阵.在它上面你可以成功地使用你的自定义函数 'cov' 它会起作用.但是当你进一步采用这种方法时,你最终会出现另一个错误.下面是第一个由于 COV 是数组而发生的.因此我认为或者我猜使用 c (combine) 是你所需要的.

I am working with the R programming language. I am trying to recreate the graphs shown in this tutorial over here : https://www.rpubs.com/cboettig/greta-gp

This tutorial shows how to make a special type of regression model for 2 variables. I am able to copy and paste the code from this tutorial and successfully make the desired graphs:

#PART 1
#load libraries
library(MASS)
library(tidyverse)

#set seed
set.seed(12345)

#create initial data
x_predict <- seq(-5,5,len=50)
l <- 1

#define functions for evaluating the covariance
SE <- function(Xi,Xj, l) exp(-0.5 * (Xi - Xj) ^ 2 / l ^ 2)
cov <- function(X, Y) outer(X, Y, SE, l)
COV <- cov(x_predict, x_predict)

#sample these functions, place them into a data frame and plot
values <- mvrnorm(200, rep(0, length=length(x_predict)), COV)
dat <- data.frame(x=x_predict, t(values)) %>%
  tidyr::pivot_longer(-x, names_to = "rep", values_to = "value") %>% 
  mutate(rep = as.numeric(as.factor(rep)))

ggplot(dat,aes(x=x,y=value)) +
  geom_line(aes(group=rep), color =  rgb(0.7, 0.1, 0.4), alpha = 0.4) 

#PART2

#create new data

obs <- data.frame(x = c(-4, -3, -1,  0,  2),
                  y = c(-2,  0,  1,  2, -1))

#repeat steps from part 1

cov_xx_inv <- solve(cov(obs$x, obs$x))
Ef <- cov(x_predict, obs$x) %*% cov_xx_inv %*% obs$y
Cf <- cov(x_predict, x_predict) - cov(x_predict, obs$x)  %*% cov_xx_inv %*% cov(obs$x, x_predict)

values <- mvrnorm(200, Ef, Cf)

dat <- data.frame(x=x_predict, t(values)) %>%
  tidyr::pivot_longer(-x, names_to = "rep", values_to = "value") %>% 
  mutate(rep = as.numeric(as.factor(rep)))


gp <- data.frame(x = x_predict, Ef = Ef, sigma = 2*sqrt(diag(Cf)) )

ggplot(dat,aes(x=x,y=value)) + 
  geom_line(aes(group=rep), color =  rgb(0.7, 0.1, 0.4), alpha = 0.2) + #REPLICATES
  geom_ribbon(data = gp, 
              aes(x, 
                  y = Ef, 
                  ymin = Ef - sigma, 
                  ymax = Ef + sigma),
              fill="grey", alpha = 0.4) +
  geom_line(dat = gp, aes(x=x,y=Ef), size=1) + #MEAN
  geom_point(data=obs,aes(x=x,y=y)) +  #OBSERVED DATA
  scale_y_continuous(lim=c(-3,3), name="output, f(x)") +
  xlab("input, x")

Now, I am trying to replicate the above tutorial for a regression model with 3 variables (1 response, 2 predictors). I tried to make the "x_predict" object have two columns instead:

x_predict_1 <- seq(-5,5,len=50)
x_predict_2 <- seq(-6,6,len=50)

l <- 1

x_predict <- data.frame(x_predict_1, x_predict_2)


COV <- cov(x_predict, x_predict)

But this produces the following error:

Error in Xi - Xj : non-numeric argument to binary operator 

This error is preventing me from creating the "values" and the "dat" objects from part 1, and I can not create the desired graphs (e.g. x_predict_1 vs values and x_predict_2 vs values). This is also preventing me from creating the desired graphs in part 2.

Can someone please show me how to fix this problem? Thanks

解决方案

I think I got the problem. First of all below is the way by which we can reproduce the error & the way you have proceed :

#PART 1
#load libraries
library(MASS)
library(tidyverse)

#set seed
set.seed(12345)

#create initial data
x_predict <- seq(-5,5,len=50)
l <- 1

#define functions for evaluating the covariance
SE <- function(Xi,Xj, l) exp(-0.5 * (Xi - Xj) ^ 2 / l ^ 2)
cov <- function(X, Y) outer(X, Y, SE, l)
COV <- cov(x_predict, x_predict)

#sample these functions, place them into a data frame and plot
values <- mvrnorm(200, rep(0, length=length(x_predict)), COV)
dat <- data.frame(x=x_predict, t(values)) %>%
  tidyr::pivot_longer(-x, names_to = "rep", values_to = "value") %>% 
  mutate(rep = as.numeric(as.factor(rep)))

ggplot(dat,aes(x=x,y=value)) +
  geom_line(aes(group=rep), color =  rgb(0.7, 0.1, 0.4), alpha = 0.4) 


x_predict_1 <- seq(-5,5,len=50)
x_predict_2 <- seq(-6,6,len=50)

l <- 1

x_predict <- data.frame(x_predict_1, x_predict_2)

COV <- cov(x_predict, x_predict)

At end of this code will end up with an error, same highlighted by Noob

Here point to note, that the cov function present in base R is redefined and set as below

cov <- function(X, Y) outer(X, Y, SE, l)

This custom function will only work with vectors/array not with data.frames which is the method used to join the x_predict_1 & x_predict_1 while extending the code.

If this custom function called on data.frame object it will always result an error as its not build to handle data.frame, it was built only for numeric vectors & arrays

Now when any new person will try to replicate it from in between, he by default will use 'cov' function from base R. which works on data.frame objects. hence its highly recommended to never re-define existing function in R, it leads to lots of confusion. If we remove the custom 'cov' function and call the cov(x_predict, x_predict) it will work without error which will be called from base R package.

So to resolve this problem, Noob you just need to use 'c' (combine) instead of 'data.frame' while joining the x_predict_1 & x_predict_2 and your problem will resolved. I am giving the full code I tried with your variables :

library(MASS)
library(tidyverse)

#set seed
set.seed(12345)

SE <- function(Xi,Xj, l) exp(-0.5 * (Xi - Xj) ^ 2 / l ^ 2)
cov <- function(X, Y) outer(X, Y, SE, l)

x_predict_1 <- seq(-5,5,len=50)
x_predict_2 <- seq(-6,6,len=50)

l <- 1

x_predict <- c(x_predict_1, x_predict_2)
head(x_predict,5)
COV <- cov(x_predict, x_predict)

values <- mvrnorm(200, rep(0, length=length(x_predict)), COV)
dat <- data.frame(x=x_predict, t(values)) %>%
  tidyr::pivot_longer(-x, names_to = "rep", values_to = "value") %>% 
  mutate(rep = as.numeric(as.factor(rep)))

ggplot(dat,aes(x=x,y=value)) +
  geom_line(aes(group=rep), color =  rgb(0.7, 0.1, 0.4), alpha = 0.4) 

The end result will be below graph. I hope this explanation will resolve your problem, if not please let me know.

here, If you does not want to use 'c' (combine) you can use cbind and create a Matrix. On it you can successfully use your custom function 'cov' it will work. but still when you go further with this approach you will end up another errors. Below is the first one occur due to COV being an array. hence I think or I guess using c (combine) is what you needed.

这篇关于(R) Xi - Xj 中的错误:二元运算符的非数字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆