如何将回归应用于 R 中的动物园数据框?
How to apply regression to a zoo data frame in R?
我有一个动物园数据框,其中一部分看起来像:
TLT PCY SHY
2015-04-15 122.1545 28.16594 84.63730
2015-04-16 123.9836 28.11196 84.72825
2015-04-17 124.4341 28.07958 84.70804
2015-04-20 125.2258 28.15514 84.74845
2015-04-21 125.9629 28.18753 84.76866
2015-04-22 126.1267 28.28469 84.79897
2015-04-23 126.4923 28.30628 84.85056
2015-04-24 124.5478 28.34947 84.81010
2015-04-27 123.3290 28.36026 84.63820
2015-04-28 124.3560 28.38185 84.83033
2015-04-29 123.5618 28.45742 84.76965
2015-04-30 122.8086 28.53299 84.73932
2015-05-01 124.0000 28.57618 84.80000
或者看跌期权看起来像:
structure(c(122.154509543852, 123.983621421486, 124.434078860684,
125.225787705634, 125.962885778852, 126.126687074014, 126.492320227327,
124.547751779181, 123.328974601471, 124.356047962478, 123.561779964634,
122.808613513252, 124, 28.1659442697104, 28.1119613477074, 28.079579550721,
28.1551370770225, 28.187532134368, 28.2846907856862, 28.3062786503438,
28.3494676400181, 28.3602615723469, 28.3818494370044, 28.4574202236651,
28.5329910103257, 28.57618, 84.6373035627333, 84.7282463827038,
84.7080380110033, 84.7484547544043, 84.7686631261047, 84.7989705363372,
84.8505569604793, 84.8100990385319, 84.6381991961199, 84.8303279995055,
84.769651411221, 84.7393234117153, 84.8), .Dim = c(13L, 3L), .Dimnames =
list(NULL, c("TLT", "PCY", "SHY")), index = structure(c(16540,
16541, 16542, 16545, 16546, 16547, 16548, 16549, 16552, 16553,
16554, 16555, 16556), class = "Date"), class = "zoo")
我想对该数据框的列应用线性回归(或一阶多项式拟合),并将结果作为一个向量,向量中的值是每列拟合线的斜率数据。有没有一种方法可以使用“apply”和匿名函数来使用“lm”执行此操作,或者是否有库函数可以执行此操作?
尝试
apply(zz, 2, function(q) coef(lm(q ~ time(q)))[2])
我有一个动物园数据框,其中一部分看起来像:
TLT PCY SHY
2015-04-15 122.1545 28.16594 84.63730
2015-04-16 123.9836 28.11196 84.72825
2015-04-17 124.4341 28.07958 84.70804
2015-04-20 125.2258 28.15514 84.74845
2015-04-21 125.9629 28.18753 84.76866
2015-04-22 126.1267 28.28469 84.79897
2015-04-23 126.4923 28.30628 84.85056
2015-04-24 124.5478 28.34947 84.81010
2015-04-27 123.3290 28.36026 84.63820
2015-04-28 124.3560 28.38185 84.83033
2015-04-29 123.5618 28.45742 84.76965
2015-04-30 122.8086 28.53299 84.73932
2015-05-01 124.0000 28.57618 84.80000
或者看跌期权看起来像:
structure(c(122.154509543852, 123.983621421486, 124.434078860684,
125.225787705634, 125.962885778852, 126.126687074014, 126.492320227327,
124.547751779181, 123.328974601471, 124.356047962478, 123.561779964634,
122.808613513252, 124, 28.1659442697104, 28.1119613477074, 28.079579550721,
28.1551370770225, 28.187532134368, 28.2846907856862, 28.3062786503438,
28.3494676400181, 28.3602615723469, 28.3818494370044, 28.4574202236651,
28.5329910103257, 28.57618, 84.6373035627333, 84.7282463827038,
84.7080380110033, 84.7484547544043, 84.7686631261047, 84.7989705363372,
84.8505569604793, 84.8100990385319, 84.6381991961199, 84.8303279995055,
84.769651411221, 84.7393234117153, 84.8), .Dim = c(13L, 3L), .Dimnames =
list(NULL, c("TLT", "PCY", "SHY")), index = structure(c(16540,
16541, 16542, 16545, 16546, 16547, 16548, 16549, 16552, 16553,
16554, 16555, 16556), class = "Date"), class = "zoo")
我想对该数据框的列应用线性回归(或一阶多项式拟合),并将结果作为一个向量,向量中的值是每列拟合线的斜率数据。有没有一种方法可以使用“apply”和匿名函数来使用“lm”执行此操作,或者是否有库函数可以执行此操作?
尝试
apply(zz, 2, function(q) coef(lm(q ~ time(q)))[2])