-1

当我尝试这些代码时收到这些错误

> fk <- function(u.vect, lambda, alpha, beta, x, k) {
+   res <- sapply(u.vect, function(u) {
+     # Cause-specific hazard
+     hk <-
+       lambda[k] * alpha[k] * (uˆ(alpha[k] - 1)) * exp(sum(unlist(beta[, k]) * x))
Error: unexpected input in:
"    hk <-
      lambda[k] * alpha[k] * (uˆ"
>     # Cumulative cause-specific hazard
>     Hk <-
+       lambda * (rep(u, length(lambda))ˆalpha) * exp((t(beta) %*% matrix(x, ncol = 1))[, 1])
Error: unexpected input in:
"    Hk <-
      lambda * (rep(u, length(lambda))ˆ"
>     # Cause-specific hazard x Overall survival
>     aux <- hk * exp(-sum(Hk))
Error: object 'hk' not found
>     return(aux)
Error: object 'aux' not found
>   })
Error: unexpected '}' in "  }"
>   return(res)
Error: no function to return from, jumping to top level
> }
Error: unexpected '}' in "}"

错误是什么意思,我该如何解决?

请帮忙,谢谢

4

1 回答 1

1

不幸的是,R 经常不能容忍非 ASCII 字符,尤其是作为运算符。我已经ˆ^(正如 MartinGal 建议的那样)替换了该函数,并且该函数可以毫无怨言地读取/解析。

fk <- function(u.vect, lambda, alpha, beta, x, k) {
  res <- sapply(u.vect, function(u) {
    # Cause-specific hazard
    hk <-
      lambda[k] * alpha[k] * (u^(alpha[k] - 1)) * exp(sum(unlist(beta[, k]) * x))
    # Cumulative cause-specific hazard
    Hk <-
      lambda * (rep(u, length(lambda))^alpha) * exp((t(beta) %*% matrix(x, ncol = 1))[, 1])
    # Cause-specific hazard x Overall survival
    aux <- hk * exp(-sum(Hk))
    return(aux)
  })
  return(res)
}
于 2021-10-19T13:22:21.697 回答