sse: Estimate the Sum of Squared Errors (SSE) from the Predicted...

Description Usage Arguments Value Examples

View source: R/sse.R

Description

Estimate the SSE using formulae provided in Chapter 16 (pg. 319) of the Traffic Flow Dynamics book (first edition) by Martin Treiber and Arne Kesting

Usage

1
sse(y_pred, y_data, type = "mix")

Arguments

y_pred

Predicted bumper-to-bumper spacing, speed or acceleration. A vector of doubles.

y_data

Observed bumper-to-bumper spacing, speed or acceleration. A vector of doubles. It should have the same length as y_pred.

type

Provide the type of SSE you want to calculate. Three options: "abs", "rel" and "mix" (default).

Value

Sum of Squared Errors. Double. Either of absolute, relative or mixed.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
# Time
last_time <- 3000 ## s
time_frame <- 0.1 ## s
Time <- seq(from = 0, to = last_time, by = time_frame)
time_length <- length(Time)



## Lead vehicle
vn1_first <- 13.9 ## first speed m/s
xn1_first <- 100 ## position of lead vehicle front center m
bn1_complete <- c(rep(0, 29500),
                 rep(-5, time_length - 29500))



#############################################
### Complete speed trajectory of Lead vehicle
#############################################

vn1_complete <- rep(NA_real_, time_length) ### an empty vector
xn1_complete <- rep(NA_real_, time_length) ### an empty vector

vn1_complete[1] <- vn1_first
xn1_complete[1] <- xn1_first

for (t in 2:time_length) {

 ### Lead vehicle calculations
 vn1_complete[t] <- vn1_complete[t-1] + (bn1_complete[t-1] * time_frame)

 vn1_complete[t] <- ifelse(vn1_complete[t] < 0, 0, vn1_complete[t])


 xn1_complete[t] <- xn1_complete[t-1] + (vn1_complete[t-1] * time_frame) +
   (0.5 * bn1_complete[t-1] * (time_frame)^2)

}



ldf <- data.frame(Time, bn1_complete, xn1_complete, vn1_complete)

## Assuming that the observed following vehicle trajectory is approximated by IDM:
obs_data <- simulate_idm(

 resolution=0.1,
 N=1,

 dfn1=ldf,
 xn1="xn1_complete",
 vn1="vn1_complete",

 xn_first=list(85),
 vn_first=list(12),
 ln=list(5),

 a=2,
 v_0=14.4,
 small_delta=1,
 s_0=4,
 Tg=1,
 b=1.5
)

# Remove the zero speed part in observed data:
obs_data <- obs_data[obs_data$vn > 0,]

# Predicting with Wiedemann model
pred_data <- simulate_wiedemann74_driver(
 resolution=0.1,
 N=1,
 dfn1=ldf,
 xn1="xn1_complete",
 vn1="vn1_complete",
 bn1="bn1_complete",
 xn_first=list(85),
 vn_first=list(12),
 ln=list(5),
 D_MAX=150,
 V_MAX=44,
 V_DESIRED=14.4,
 FAKTORVmult=0.001,
 BMAXmult=0.08,
 BNULLmult=0.25,
 BMIN=-5,
 CX=50,
 AXadd=2,
 BXadd=2,
 EXadd=2,
 OPDVadd=1.5
)

pred_data <- pred_data[1:nrow(obs_data), ]



# Now we can estimate the sum of squared errors:
sse(pred_data$sn, obs_data$sn, type = "mix")
sse(pred_data$vn, obs_data$vn, type = "abs")

durraniu/CarFollowingModels documentation built on Dec. 20, 2021, 2:15 a.m.