R 使用公式计算变量,然后使用生成的值生成新值
很难解释,但我会一步一步来 假设我有两辆车,一辆跟在另一辆后面,我有前面车的速度,我想计算两辆车之间的距离,我们可以用多个方程计算距离,我也知道后面车的初始速度和两辆车之间的距离R 使用公式计算变量,然后使用生成的值生成新值,r,loops,dplyr,tidyverse,zoo,R,Loops,Dplyr,Tidyverse,Zoo,很难解释,但我会一步一步来 假设我有两辆车,一辆跟在另一辆后面,我有前面车的速度,我想计算两辆车之间的距离,我们可以用多个方程计算距离,我也知道后面车的初始速度和两辆车之间的距离 Following_Car_Speed = 13.68490 m/s Distance = 17.024 m Lead_Car_Speed = c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577, 13.923959, 13.945661, 13.91961
Following_Car_Speed = 13.68490 m/s
Distance = 17.024 m
Lead_Car_Speed = c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577, 13.923959,
13.945661, 13.919619, 13.897917, 14.002257, 14.002257, 13.980556,
13.980556, 14.067536, 14.063195, 14.080556, 14.123959, 14.163021,
14.236806, 14.167188)
Delta_Speed = Lead_Car_Speed[1]-Following_Car_Speed = 13.784896-13.68490 = 0.1
Gap <- 1.554 + Following_Car_Speed*0.878- (Following_Car_Speed*Delta_Speed)/(2*sqrt(0.8418*0.8150))=
1.554+ 13.68490*0.878- (13.68490*0.1)/(2*sqrt(0.8418*0.8150) = 12.74325
Acceleration <- 0.8418*(1-(Following_Car_Speed/29.2)^3.52-(Gap/Distance)^2)=0.3116923
我已经添加了lead\u car\u speed的lead,我还想使用新的距离和新的速度,所以我把它做成了一个列表,并将其放入。initla您的操作取决于迭代或某种递归来完成,因为可能需要NA、Inf等 我的另一个版本是将函数分解为更简单的单元以进行扩展,并对更大的示例进行单元测试,或者使用另一个函数调用进行封装 我假设由于某种原因,您的var距离是固定的,并且没有提供预期的输出,因此我无法完全验证函数
Following_Car_Speed <- 13.68490
Lead_Car_Speed <- c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577,
13.923959, 13.945661, 13.919619, 13.897917, 14.002257,
14.002257, 13.980556, 13.980556, 14.067536, 14.063195,
14.080556, 14.123959, 14.163021, 14.236806, 14.167188)
Distance <- 17.024
#init
i <- length(Lead_Car_Speed)
Delta_Speed <- c(0.1, vector(mode = "numeric", length = i-1))
Gap <- c(12.74325, vector(mode = "numeric", length = i-1))
Acceleration <- c(0.3116923, vector(mode = "numeric", length = i-1 ))
Following_Car_Speed <- c(Following_Car_Speed, vector(mode="numeric", length = i-1 ))
cal.following.car.speed <- function(Following_Car_Speed, Acceleration){
#calculate and return current following-car-speed: the i-th following-car-speed
current.follow.car.speed <- Following_Car_Speed + Acceleration*0.1
return(current.follow.car.speed)
}
cal.delta.speed <- function(Lead_Car_Speed, following.car.speed){
#calculate and return current delta speed: the i-th delta speed
current.delta.speed <- Lead_Car_Speed - following.car.speed
return(current.delta.speed)
}
cal.gap <- function(Following_Car_Speed, Delta_Speed){
#calculate and return current gap: the i-th gap
current.gap <- 1.554 + Following_Car_Speed*0.878 - (Following_Car_Speed*Delta_Speed)/(2*sqrt(0.8418*0.8150))
return(current.gap)
}
cal.acceleration <- function(Following_Car_Speed, Gap, Distance){
#calculate and return current acceleration: the i-th acceleration
current.acceleration <- 0.8418*(1-(Following_Car_Speed/29.2)^3.52-(Gap/Distance)^2)
return(current.acceleration)
}
#main
counter <- 1
while(counter != i){
if(counter == 1) {counter = counter + 1} #skip 1st ith as we have init
else{
Gap[counter] <- cal.gap(Following_Car_Speed[counter-1], Delta_Speed[counter-1])
Acceleration[counter] <- cal.acceleration(Following_Car_Speed[counter-1], Gap[counter-1], Distance)
Following_Car_Speed[counter] <- cal.following.car.speed(Following_Car_Speed[counter-1], Acceleration[counter])
Delta_Speed[counter] <- cal.delta.speed(Lead_Car_Speed[counter], Following_Car_Speed[counter])
counter = counter + 1
}
}
cbind(Delta_Speed, Gap, Acceleration, Following_Car_Speed)
下面是一种使用purr包(tidyverse的一部分)中的累积的简单方法 首先,我定义了一个函数myfun,它更新了以下车辆速度fcs
myfun <- function(fcs, lcs, di){
ds <- lcs - fcs
gap <- 1.554 + fcs*0.878 - fcs * ds / (2*sqrt(0.8418*0.8150))
acc <- 0.8418 * (1 - (fcs / 29.2)^3.52 - (gap / di)^2)
fcs_new <- fcs + acc * 0.1
return(fcs_new)
}
library(tidyverse)
tibble(lead_car_speed = c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577, 13.923959,
13.945661, 13.919619, 13.897917, 14.002257, 14.002257, 13.980556,
13.980556, 14.067536, 14.063195, 14.080556, 14.123959, 14.163021,
14.236806, 14.167188)) %>%
mutate(following_car_speed = accumulate(lead_car_speed, myfun, .init = 13.68490, di = 17.024)[-1])^
# A tibble: 20 x 2
lead_car_speed following_car_speed
<dbl> <dbl>
1 13.8 13.7
2 13.7 13.7
3 13.9 13.8
4 13.9 13.8
5 13.9 13.8
6 13.9 13.9
7 13.9 13.9
8 13.9 13.9
9 13.9 13.9
10 14.0 14.0
11 14.0 14.0
12 14.0 14.0
13 14.0 14.0
14 14.1 14.1
15 14.1 14.1
16 14.1 14.1
17 14.1 14.1
18 14.2 14.1
19 14.2 14.2
20 14.2 14.2
如果距离也发生变化,您可以使用累加2而不是累加。嗨,第一次迭代后距离是如何更新的?对不起,这是我的错,我忘了写它了,它必须更新,它是旧距离加上新旧速度增量的平均值乘以0.1您可以更新您的答案吗?我想添加距离,并将其作为一个新列,如以下速度。我将在有时间时尝试更新。现在,如果你的数据真的很大,我甚至建议你使用rcpp软件包。我试图了解更多关于Purr和tidyverse的信息,但掌握它需要时间,你的意思是我可以使用rcpp做同样的事情吗?如果你需要更多有挑战性的问题,这里是,这是主要的问题,但没有人回答,所以我试着把它做成一个小问题。C++应该快得多。检查rcpp包。感谢您的时间,我在使用任何迭代时遇到的问题是,我的模拟模型有大数据,如果我使用您的答案会非常慢,Cett的答案非常有用
myfun <- function(list, lcs,lcs2){
ds <- lcs - list[[1]]
Distance <- list[[1]]*D_Time - (list[[1]] * ds) / (2*sqrt(M_Acc*Com_Acc))
if (Distance < 0|is.na(Distance)) {Distance <- 0}
gap <- Gap_J + Distance
acc <- M_Acc * (1 - (list[[1]] / D_Speed)^Beta - (gap / list[[2]])^2)
fcs_new <- list[[1]] + acc * 0.1
ds_new <- lcs2- fcs_new
di_new <- list[[2]]+(ds_new+ds)/2*0.1
return(list(Speed = fcs_new,Distance = di_new))
}
Generated_Data <- data %>%group_by(Driver,FileName)%>%
mutate(Speed_Distance_Calibrated = accumulate2( .init = list(Filling_Speed[1],
Filling_Range[1]),.x = Lead_Veh_Speed_F,.y = Lead_Veh_Speed_F2, myfun)[-1])%>%ungroup()
Following_Car_Speed <- 13.68490
Lead_Car_Speed <- c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577,
13.923959, 13.945661, 13.919619, 13.897917, 14.002257,
14.002257, 13.980556, 13.980556, 14.067536, 14.063195,
14.080556, 14.123959, 14.163021, 14.236806, 14.167188)
Distance <- 17.024
#init
i <- length(Lead_Car_Speed)
Delta_Speed <- c(0.1, vector(mode = "numeric", length = i-1))
Gap <- c(12.74325, vector(mode = "numeric", length = i-1))
Acceleration <- c(0.3116923, vector(mode = "numeric", length = i-1 ))
Following_Car_Speed <- c(Following_Car_Speed, vector(mode="numeric", length = i-1 ))
cal.following.car.speed <- function(Following_Car_Speed, Acceleration){
#calculate and return current following-car-speed: the i-th following-car-speed
current.follow.car.speed <- Following_Car_Speed + Acceleration*0.1
return(current.follow.car.speed)
}
cal.delta.speed <- function(Lead_Car_Speed, following.car.speed){
#calculate and return current delta speed: the i-th delta speed
current.delta.speed <- Lead_Car_Speed - following.car.speed
return(current.delta.speed)
}
cal.gap <- function(Following_Car_Speed, Delta_Speed){
#calculate and return current gap: the i-th gap
current.gap <- 1.554 + Following_Car_Speed*0.878 - (Following_Car_Speed*Delta_Speed)/(2*sqrt(0.8418*0.8150))
return(current.gap)
}
cal.acceleration <- function(Following_Car_Speed, Gap, Distance){
#calculate and return current acceleration: the i-th acceleration
current.acceleration <- 0.8418*(1-(Following_Car_Speed/29.2)^3.52-(Gap/Distance)^2)
return(current.acceleration)
}
#main
counter <- 1
while(counter != i){
if(counter == 1) {counter = counter + 1} #skip 1st ith as we have init
else{
Gap[counter] <- cal.gap(Following_Car_Speed[counter-1], Delta_Speed[counter-1])
Acceleration[counter] <- cal.acceleration(Following_Car_Speed[counter-1], Gap[counter-1], Distance)
Following_Car_Speed[counter] <- cal.following.car.speed(Following_Car_Speed[counter-1], Acceleration[counter])
Delta_Speed[counter] <- cal.delta.speed(Lead_Car_Speed[counter], Following_Car_Speed[counter])
counter = counter + 1
}
}
cbind(Delta_Speed, Gap, Acceleration, Following_Car_Speed)
myfun <- function(fcs, lcs, di){
ds <- lcs - fcs
gap <- 1.554 + fcs*0.878 - fcs * ds / (2*sqrt(0.8418*0.8150))
acc <- 0.8418 * (1 - (fcs / 29.2)^3.52 - (gap / di)^2)
fcs_new <- fcs + acc * 0.1
return(fcs_new)
}
library(tidyverse)
tibble(lead_car_speed = c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577, 13.923959,
13.945661, 13.919619, 13.897917, 14.002257, 14.002257, 13.980556,
13.980556, 14.067536, 14.063195, 14.080556, 14.123959, 14.163021,
14.236806, 14.167188)) %>%
mutate(following_car_speed = accumulate(lead_car_speed, myfun, .init = 13.68490, di = 17.024)[-1])^
# A tibble: 20 x 2
lead_car_speed following_car_speed
<dbl> <dbl>
1 13.8 13.7
2 13.7 13.7
3 13.9 13.8
4 13.9 13.8
5 13.9 13.8
6 13.9 13.9
7 13.9 13.9
8 13.9 13.9
9 13.9 13.9
10 14.0 14.0
11 14.0 14.0
12 14.0 14.0
13 14.0 14.0
14 14.1 14.1
15 14.1 14.1
16 14.1 14.1
17 14.1 14.1
18 14.2 14.1
19 14.2 14.2
20 14.2 14.2