简体   繁体   English

写一个函数来计算R中的PDF?

[英]Write a function to calculate the PDF in R?

I'm to write a function to calculate the PDF of and it's been suggested that I use the if/elseif/else statements. 我要编写一个函数来计算PDF,建议使用if / elseif / else语句。 However whenever I try and do so, I keep getting error messages but I'm not sure what I'm doing wrong? 但是,每当尝试这样做时,我都会不断收到错误消息,但是我不确定自己做错了什么?

This is the PDF that the function is supposed to calculate: 这是函数应该计算的PDF:

fx = 0.3 if (0<=x<1) 如果(0 <= x <1),则fx = 0.3
0.1 if (1<=x<2) 如果(1 <= x <2)为0.1
0.25 if (2<=x<3) 如果(2 <= x <3)则为0.25
0.15 if (3<=x<4) 如果(3 <= x <4)为0.15
0.2 if (4<=x<5) 如果(4 <= x <5)为0.2
0 otherwise 否则为0

This is my code: 这是我的代码:

    fx = function(x)
    { 
    if (0<=x<1) {
    pdf=0.3
    } elseif (1<=x<2) {
    pdf=0.1
    } elseif (2<=x<3) {
    pdf=0.25
    } elseif (3<=x<4) {
    pdf=0.15
    } elseif (4<=x<5) {
    pdf=0.2
    } else 
    pdf=0

    pdf
    }

I have checked my '}' but they all seem appropriately placed. 我已经检查了我的'}',但它们似乎都放在适当的位置。 I've tried changing 'pdf' to 'fx' but that doesn't work. 我尝试将'pdf'更改为'fx',但这不起作用。 Where am I going wrong? 我要去哪里错了?

fx <- function(x) c(0, 0.3,0.1,0.25,0.15,0.20, 0)[findInterval(x, c(-Inf, 0:5, Inf))]

The findInterval function returns the position of its first argument from within a sequence of intervals defined by its second argument and can be used to select a probability from a vector. findInterval函数从其第二个自变量定义的间隔序列中返回其第一个自变量的位置,并可用于从向量中选择概率。 The values outside the domain of support get chosen as 0 values as requested. 支持范围之外的值将根据请求选择为0值。 It is particularly useful in cases like this where the lower bounds are closed, since the default for the cut function is to have upper bounds as closed. 在这种情况下,下边界是封闭的,这特别有用,因为cut函数的默认值是上边界是封闭的。

there were two problems with your code 您的代码有两个问题

1/ Expressions such as a <= x < b are legal in maths but not in code. 1 /诸如a <= x <b的表达式在数学上是合法的,但在代码中不是。 In code you need a <=x && x < b 在代码中,您需要<= x && x <b

2/ You need to use "else if" rather than "elseif" 2 /您需要使用“ else if”而不是“ elseif”

This code works 此代码有效

fx = function(x)
{ 
if (0<=x && x<1) {
pdf=0.3    
} else if (1<=x && x<2) {
pdf=0.1
} else if (2<= x&& x<3) {
pdf=0.25
} else if (3<=x && x<4) {
pdf=0.15
} else if (4<= x&& x<5) {
pdf=0.2
} else 
pdf=0

pdf
}

Also, as an optimization, you should only repeatedly need to test that x < b, not the a. 另外,作为优化,您应该只需要反复测试x <b,而不是a。 And you shouldn't need a pdf variable. 而且您不需要pdf变量。 This leads to the following code, but I'm guessing that the built-in function that was posted above is more efficient. 这将导致以下代码,但是我猜想上面发布的内置函数效率更高。

fx = function(x)
{ 
if (x<0) 0
else if (x<1) 0.3
else if (x<2) 0.1
else if (x<3) 0.25
else if (x<4) 0.15
else if (x<5) 0.2
else 0
}

You could use stepfun to create a step function (which is what your PDF is) 您可以使用stepfun创建一个step函数(这就是您的PDF)

fx <- stepfun(x = 0:5, y = c(0,0.3,0.1,0.25,0.15,0.20,0))

If you want a vectorized version, then you can use Vectorize 如果需要矢量化版本,则可以使用Vectorize

vfx <- Vectorize(fx)

stepfun objects can be plotted nicely using plot (see https://stackoverflow.com/a/16072697/1385941 for a related example) stepfun可以使用plot很好地绘制stepfun对象(有关示例,请参见https://stackoverflow.com/a/16072697/1385941

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM