Vote count:
0
I am trying to numerically integrate the square of the density of a generalized extreme value distribution. So far I have tried this (partly based on the dgev function of the evd package):
So far I have this
f <- function(x){
z <- (x - loc)/sc
if (sh == 0) {t <- log(1/sc) - z - exp(-z); return(exp(t)^2)}
else if (sh*z < 0) {return(0)}
else {t <- (1 + sh*z); return(exp(log(1/sc) - t^(-1/sh) - (1/sh + 1)*log(t))^2)}
}
loc <- 10; sc <- 2; sh <- 0
integrate(f, -Inf, Inf, rel.tol = 1e-6)$value # no error message
loc <- 10; sc <- 2; sh <- 0.1
integrate(f, -Inf, Inf, rel.tol = 1e-6)$value # error message
As indicated in the code, if sh is not 0, I get an error message, saying that
Fehler in integrate(f, -Inf, Inf, rel.tol = 1e-06) :
evaluation of function gave a result of wrong length
Zusätzlich: Warnmeldung:
In if (sh * z < 0) { :
Bedingung hat Länge > 1 und nur das erste Element wird benutzt
Fehler in integrate(f, -Inf, Inf, rel.tol = 1e-06) :
evaluation of function gave a result of wrong length
Why does this happen?
I have also tried using the dgev function from the evd package in integrate, this works fine, but I would like to understand what I have done wrong in my code.
Thanks for your help in advance
asked 38 secs ago
Using integrate() and function with if loop
Aucun commentaire:
Enregistrer un commentaire