简体   繁体   中英

Debugging simple R code

For a homework assignment I'm constructing a simple code. R keeps returning that a x0 is missing in evaluating p2. I can't figure out why, because it seems like I specified it correctly in the function Pareto.

lab1 <- 50; mu <- 1; sigma <- 1 alpha1 <- 1 ; beta1 <- 1
lab2 <- 3; mu <- 5; sigma <- 5
alpha2 <- (1+sqrt(2)); x0 <- (10- 5* sqrt(2))
ES1 <- lab1 * alpha1/beta1; VarS1 <- lab1 * alpha1/(beta1^2)
ES2 <- lab2 * alpha2*x0/(alpha2-1); VarS2 <- lab2 * alpha2*x0^2/((alpha2-1)^2*(alpha2-2))
MM <- trunc(ES1 + ES2 + 10*sqrt(VarS1 + VarS2))
p1 <- pgamma((0:(MM-1)+0.5), alpha1, beta1) ##cdf
p1 <- c(p1,1) - c(0,p1) ## pdf
Pareto <- function (x, alpha2, x0) ifelse(x<x0 , 0, 1 - (x0/x)^alpha2)
p2 <- integrate(Pareto, lower = 0, upper = (MM-1)+0.5) ##cdf
p2 <- c(p2,1) - c(0,p2) ## pdf

Many thanks in advance!

Regards, Vincent

It seems to me that you need to supply the arguments of your Pareto function within the integrate function. Like that

p2 <- integrate(Pareto, lower = 0, upper = (MM-1)+0.5, x0=x0, alpha2=alpha2) ##cdf

And to access the value of after integrate you have to access it with the dollar sign

p2 <- c(p2$value,1) - c(0,p2$value) ## pdf

解决方法是指定alpha2和x0的默认值,如下所示,如果未指定R,则R将使用这些值。

Pareto <- function (x, alpha2 = (1+sqrt(2)), x0=(10- 5* sqrt(2))) ifelse(x<x0 , 0, 1 - (x0/x)^alpha2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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