简体   繁体   English

循环 function 通过 R 中的连续乘法 function 获得指数倍数

[英]A loop function for an exponential multiple by a continuous multiplication function in R

I try to calculate values from the function:我尝试从 function 计算值:

在此处输入图像描述 And the loop function I write so far is:到目前为止我写的循环 function 是:

However, even though it can run, but when I apply this function to my data, there is an error says "Error: unused argument (.)" Maybe because y[q,k] <- ((3+0.01*(q-1))^(-(1:k)))*sum(cumprod(px[1:k])) is wrong?但是,即使它可以运行,但是当我将这个 function 应用到我的数据时,有一个错误说“错误:未使用的参数 (.)”可能是因为y[q,k] <- ((3+0.01*(q-1))^(-(1:k)))*sum(cumprod(px[1:k]))错了吗? But it looks like the equation I want.但它看起来像我想要的方程。 Anyone know how can I fix this problem?任何人都知道我该如何解决这个问题? Thanks in advance.提前致谢。

Y <- function(x){
  k <- 50-x
  Cset <- seq(0,0.2,0.01)
  y <- matrix(length(Cset)*k,ncol = k)
  for(C in seq_along(Cset)){
    for(s in 1:k){
      y[C,s] <- sum((3+C)^(-s)*cumprod(data$Bx[1:s]))
    }
  }
  y
}

Y(x=30)

And the error shows错误显示

Error in `[<-`(`*tmp*`, C, s, value = sum((3 + C)^(-s) * cumprod(data$px[1:s]))) : 
  subscript out of bounds

Maybe you can try the code below也许你可以试试下面的代码

Y <- function(x){
  k <- 50-x
  Cset <- seq(0,0.2,0.01)
  y <- matrix(length(Cset)*k,ncol = k)
  for(C in seq_along(Cset)){
    for(s in 1:k){
      y[C,s] <- (3+C)^(-s)*sum(cumprod(Bx[1:s]))
    }
  }
  y
}

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

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