【发布时间】:2018-06-08 10:53:08
【问题描述】:
我想根据一系列角度 (beta) 计算以弧度 (theta) 为单位的平均角度,这些角度存储在名为“a”的数据框中:
time beta theta theta2
1 0 na na
2 2.426551575 na na
3 5.890842799 0.678069689 0.678069689
4 0.530641823 0.854950297 0.854950297
5 4.794960386 -0.449975202 5.833210105
6 1.271620156 0.104679019 0.104679019
7 5.789548201 -0.236747291 6.046438017
8 1.053579352 0.610520801 0.610520801
9 0.095112206 0.218351484 0.218351484
10 6.108843722 0.324783324 0.324783324
beta 以弧度为单位,并且是从包装的 gauchy 分布中随机抽取的。我想计算 thetas(实际上是 theta2,以弧度为单位)。我通过蛮力完成了这些,将弧度转换为正负单位,pi/-pi 为 180 度,但正在寻找一种更优雅的方法。
我曾希望这会起作用(调用循环和 dplyr 包):
a$theta<-mean.circular(c(a$beta,lag(a$beta),lag(a$beta,2))
但这似乎只返回 beta 的最后一个值。我已经尝试过子集并且也在循环中(我有另一个名为 time 的字段,它是一个递增的整数)
time beta
1 0
2 2.426551575
3 5.890842799
4 0.530641823
5 4.794960386
6 1.271620156
7 5.789548201
8 1.053579352
9 0.095112206
10 6.108843722
theta<-0
bset<-c(0,0,0)
for (i in time){
bset<-ifelse(i<3,bset,df$beta[(i-2):i])
thetai<-mean.circular(bset)
theta<-c(theta,ifelse(is.na(thetai),0,thetai))
theta<-theta[2:(length(time)+1)]
df<-cbind(df,theta)
}
但这也行不通。谁能告诉我我做错了什么?
【问题讨论】:
标签: r moving-average arima