F# 中的滞后变量

lagging variables in F#

我有以下代码:

let years = [|1990 .. 2010|]
let rand = System.Random()
let gold = [ for i in years do yield rand.NextDouble()]
let silver = [ for i in gold do yield 2.0 * i + rand.NextDouble()]
let x = Frame.ofColumns["gold"  => Series(years, gold);
                    "silver" => Series(years, silver) ]

我想在 "lagged" 白银上回归黄金。我如何编辑下面的代码,以便我在滞后的白银上回归黄金(白银阵列向后移动一个)

let myresult = R.lm(formula = "gold~silver", data = (x |> R.as_data_frame)) 
R.summary(myresult)

你可以用Series.shift 1把一个系列的数据按指定的方向移动,所以我觉得你可以按如下方式构建框架:

let x = 
  [ "gold"  => Series(years, gold);
    "silver" => (Series(years, silver) |> Series.shift 1) ]
  |> Frame.ofColumns

此外,您不需要 R.as_data_frame 调用。这是自动发生的:-)

let myresult = R.lm(formula = "gold~silver", data = x) 
R.summary(myresult)