curve fitting - How to apply regression to a zoo data frame in R? -


i have zoo data frame, piece of looks like:

                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 

or put looks like:

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

i want apply linear regression (or first-order polynomial fitting) columns of data frame , have result vector values in vector being slope of fitted line each column of data. there way use ‘apply' , anonymous function ‘lm', or there library function out there this?

try

 apply(zz, 2, function(q) coef(lm(q ~ time(q)))[2]) 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -