R中的错误:当我尝试应用外部函数时: [英] An Error in R: When I try to apply outer function:

查看:89
本文介绍了R中的错误:当我尝试应用外部函数时:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:
步骤1:定义一个反函数,稍后我将使用

Here is my code: Step1: Define a inverse function which I will use later

inverse = function (f, lower = -100, upper = 100) {
  function (y) uniroot((function (x) f(x) - y), lower = lower, upper = upper)[1]
}

Step2:这是我的函数及其反函数:

Step2: Here is my functions and their inverse:

F1<-function(x,m1,l,s1,s2){l*pnorm((x-m1)/s1)+(1-l)*pnorm((x+m1)/s2)}

F1_inverse = inverse(function(x) F1(x,1,0.1,2,1) , -100, 100)

F2<-function(x,m2,l,s1,s2){l*pnorm((x-m2)/s1)+(1-l)*pnorm((x+m2)/s2)}

F2_inverse = inverse(function(x) F1(x,1,0.1,2,1) , -100, 100)

第3步:这是结合以上功能的最终函数(我确定该函数正确):

Step3: Here is my final function which combines the above functions (I am sure the function is correct):

copwnorm<-function(x,y,l,mu1,mu2,sd1,sd2) {
  (l*dnorm(((F1_inverse(pnorm(x))$root-mu1)/sd1))*
    dnorm(((F2_inverse(pnorm(y))$root-mu2)/sd1)))
}

Step4:我想在Step 中为该函数创建轮廓图,在此处输入代码 3:

Step4: I want to create a contour plot for the function in Stepenter code here3:

x<-seq(-2,2,0.1)
y<-seq(-2,2,0.1)

z<-outer(x,y,copwnorm)

contour(x,y,z,xlab="x",ylab="y",nlevels=15)

这里是问题出在我试图应用函数external(x,y,copwnorm),它给我一个错误:'zeroin'中的函数值无效。我可以问一下如何解决这个问题吗?

Here is the problem comes in, when I tried to apply function outer(x,y,copwnorm), it gives me an error:invalid function value in 'zeroin'. May I ask how to solve this problem?

推荐答案

我认为假设 outer(x,y,FUN)为每个必需的对 x [i]调用一次函数参数( FUN ) ] y [j] 。实际上,在创建所有可能的对并组合每个元素之后,外部仅调用 FUN 一次 x 和每个 y 元素的方式,类似于函数 expand.grid

I believe it is a very commom misconception to assume that outer(x, y, FUN) calls the function parameter (FUN) once for each required pair x[i] and y[j]. Actually, outer calls FUN only once, after creating all possible pairs, combining every element of x with every element of y, in a manner similar to the function expand.grid.

我将通过一个示例来说明这一点:考虑一下此函数,它是产品的包装,每次打印时都会显示一条消息

I'll show that with an example: consider this function, which is a wrapper for the product and print a message every time it's called:

f <- function(x,y)
{
    cat("f called with arguments: x =", capture.output(dput(x)), "y =", capture.output(dput(y)), "\n")

    x*y
}

此函数是自然矢量化的,因此我们可以使用矢量参数来调用它: / p>

This function is "naturally" vectorized, so we can call it with vector arguments:

> f(c(1,2), c(3,4))
f called with arguments: x = c(1, 2) y = c(3, 4) 
[1] 3 8

使用外部

> outer(c(1,2), c(3,4), f)
f called with arguments: x = c(1, 2, 1, 2) y = c(3, 3, 4, 4) 
     [,1] [,2]
[1,]    3    4
[2,]    6    8

注意生成的组合。

如果我们不能保证函数可以处理向量参数,那么有一个简单的技巧以确保该功能对于组合中的每对仅调用一次: Vectorize 。这将创建另一个函数,该函数将对参数中的每个元素调用一次原始函数:

If we can't guarantee that the function can handle vector arguments, there is a simple trick to ensure the function gets called only once for each pair in the combinations: Vectorize. This creates another function that calls the original function once for each element in the arguments:

> Vectorize(f)(c(1,2),c(3,4))
f called with arguments: x = 1 y = 3 
f called with arguments: x = 2 y = 4 
[1] 3 8

所以我们可以制作一个安全的 外部

So we can make a "safe" outer with it:

> outer(c(1,2), c(3,4), Vectorize(f))
f called with arguments: x = 1 y = 3 
f called with arguments: x = 2 y = 3 
f called with arguments: x = 1 y = 4 
f called with arguments: x = 2 y = 4 
     [,1] [,2]
[1,]    3    4
[2,]    6    8

在这种情况下,结果相同,因为 f 是用矢量化方式编写的,即,因为 * 是矢量化的。但是,如果您在编写函数时没有考虑到这一点,则直接在外部中使用它可能会失败,或者(更糟糕的)结果也会出错。

In this case, the results are the same because f was written in a vectorized way, i.e., because "*" is vectorized. But if your function is not written with this in mind, using it directly in outer may fail or (worse) may give wrong results.

这篇关于R中的错误:当我尝试应用外部函数时:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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