如何使先前的循环值在当前迭代中生效,并且#34; apply" ("申请" - 家庭版" for"循环)?

时间:2018-03-23 09:05:30

标签: r loops for-loop apply

for循环:

N <- 500; 
ro <- 0.6; a <- 1
set.seed(1)
v <- ts(rnorm(N,0,1))

# [1] -0.626453811  0.183643324 -0.835628612  1.595280802  0.329507772 
# [496] -1.108909998  0.307566624 -1.106894472  0.347653649 -0.873264535

y <- ts(rep(0,N)) # y[1]:=0 defined
for (t in 2:500){ y[t] <- a + ro*y[t-1] + v[t] }

y
# [1]  0.00000000  [2] 1.18364332  
# [3] 0.87455738=1+0.6*1.18364332+(-0.835628612)  
# [4] 3.12001523=1+0.6*0.87455738+(1.595280802)
# [499]  2.55513301  1.65981527

mean(y) #2.549763

我想将上述for循环转换为apply-family版本:

N <- 500; ro <- 0.6; a <- 1
set.seed(1)
v <- as.data.frame(rnorm(N,0,1)) 
# 1: -0.626453811 2: 0.183643324 3: -0.835628612 4: 1.595280802 5: 0.329507772
# 496: -1.108909998 497: 0.307566624 498: -1.106894472 499: 0.347653649 500:-0.873264535
y <- as.data.frame(c(y1=0,rep(0,N-1))) # index starts at 1.  y[1]:=0 defined
y <- c(y[1,], unlist(apply(as.matrix(2:500), 1, function(t) { y[t,] <- a + ro*y[t-1,] + v[t,] })))
y
# [1]  0.000000000  [2] 1.183643324  
# [3] 0.164371388=1+0.6*0+(-0.8356286124) 
# [4] 2.595280802=1+0.6*0+(1.5952808025)
# [496] -0.108909998  1.307566624 -0.106894472  1.347653649  0.126735465

mean(y) # 1.021897

以上代码未在for循环中给出结果。我发现了什么是错的:在apply中,在迭代方程中,不使用y的先前值;相反,y <- as.data.frame(c(y1=0,rep(0,N-1)));即yt=0用于所有t

如何成功申请家庭?

2 个答案:

答案 0 :(得分:2)

这里有一个递归 function,与你的循环相同:

my.fct <- function(t, initial.value){
  if(t == 1){
    return(initial.value)
  }
  if(t > 1){
    vec <- my.fct(t-1, initial.value)
    val <- vec[length(vec)]
    newval <- a + ro*val + v[t]
    newvec <- c(vec, newval)
    return(newvec)
  }
}
# Indeed it yields the same result:
> sum(my.fct(N, 0) == y)-N
[1] 0

你也可以随时传递a; ro; v作为参数:

N <- 500; ro <- 0.6; a <- 1
set.seed(1)
v <- ts(rnorm(N,0,1))
my.fct(500,0) # yields exactly the same values in the "for" loop 

比较表现:

# function
start.time <- Sys.time()
y <- ts(my.fct(N, 0))
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
Time difference of 0.006044865 secs

# loop
start.time <- Sys.time()
y <- ts(rep(0,N))
for (t in 2:500){ y[t] <- a + ro*y[t-1] + v[t] }
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
Time difference of 0.01205611 secs

答案 1 :(得分:1)

AR(1)过程:

$$ y_t = \ alpha + \ rho y_ {t-1} + \ nu_t,\ hspace {1cm} t = 2,...,n + 1 $$ $$(\ nu_t \ sim N(0,1))$$

(如果在N obs中包含初始$$ y_1 $$值,t可以在t = n时最终确定)

通过math'l等价物: $$ y_t =ρ^ t(\ frac {y_1} {\ rho} + \ sum_ {j = 2} ^ {t} \ frac {\ alpha + \ nu_ {j}} {\ rho ^ j})(t = 2,...,n; y_1是预先指定的)$$

解决方案1(亨利的优雅):

rm(list = ls()) # Clear workspace by deleting all objects
N <- 500; ro <- 0.6; a <- 1
set.seed(1)
v <- ts(rnorm(N,0,1))
y <- ts(rep(0,N)) # y[1]:=0 defined
y[-1] <- ro^(2:N) * (y[1]/ro + cumsum((a+v[-1]) / ro^(2:N)))
y # yields exactly the same values in the "for" loop 
(mean(y)) # 2.549763

$$ \ rho ^ t \ rightarrow 0 $$作为$$ t \ rightarrow \ infty $$,甚至很早:0.6 ^ 1458 = 4.940656e-324     ; 0.6 ^ 1459 = 0     这打击了模型。当N = 1400时,得到y [1388] = 1.114; 1389 ... 1396:天道酬勤; 1397 ... 1400:NaN的

解决方案2(Nate的形式主义):

rm(list = ls()) #  Clear workspace by deleting all objects
my.fct <- function(t, initial.value){
  if(t == 1){ return(initial.value) }
  if(t > 1){
    vec <- my.fct(t-1, initial.value)
    val <- vec[length(vec)]
    newval <- a + ro*val + v[t]
    newvec <- c(vec, newval)
    return(newvec)
  }
}

N <- 500; ro <- 0.6; a <- 1
set.seed(1)
v <- ts(rnorm(N,0,1))
my.fct(500,0) # yields exactly the same values in the "for" loop
mean(my.fct(500,0)) # 2.549763

N = 833有效。 N = 834为Error: evaluation nested too deeply: infinite recursion / options(expressions=)?提供默认options(expressions=5000)

(我无法弄清楚如何在上面的乳胶中使用数学和文本进行内联)