F#中的滞后变量

时间:2015-04-30 23:12:56

标签: f# deedle

我有以下代码:

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) ]

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

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

1 个答案:

答案 0 :(得分:2)

您可以使用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)