In aggregate() , as is often the case for many R functions that apply other R functions to subsets of data, you name the function you want to apply, in this case adding FUN = cov to your aggregate() call. You can then pass arguments to this function as part of a special argument ...
You can pass data[, "Mkt.RF"]) as the y argument to the cov() function, so something like this should work:
cov.temp <- aggregate(data[, 1:100], as.yearmon, FUN = cov, y = data[, "Mkt.RF"])
However, in this case this does not work the way you need to handle the nature of zoo data and be able to subset data[, "Mkt.RF"] similarly to another data[,1:100]1 columns are broken up by aggregate () `. Therefore, an alternative is to specify the inline function, for example:
cov.temp <- aggregate(data[, 1:100], as.yearmon, FUN = function(x) cov(x, y = data[index(x), "Mkt.RF"]))
Here is an example that should appear out of the box:
library("zoo") dat <- data.frame(matrix(rnorm(365*10*6), ncol = 6)) Dates <- seq.Date(from = as.Date("1963-07-01"), by = "days", length = 365*10) dat2 <- zoo(dat, order.by = Dates)
What gives us:
> head(dat2) X1 X2 X3 X4 X5 X6 1963-07-01 0.30910867 0.5539864 0.6433690 0.20608146 -1.7706003 -0.4607610 1963-07-02 -0.02519616 -0.1856305 1.0419578 1.01319153 0.8671110 0.1196251 1963-07-03 1.56464024 0.4980238 0.2976338 0.05654036 0.4984225 -1.4626501 1963-07-04 -0.24028698 -1.4365257 0.5707873 -0.05851961 -0.7176343 0.1233137 1963-07-05 -0.87770815 -0.5217949 -2.4875626 -0.08200408 -0.6121038 -0.3881126 1963-07-06 -0.53660576 -1.1098966 2.7411511 -1.37106883 -0.5891641 1.6322411
Now let's assume that X6 is your "Mkt.RF" column, and we will summarize over dat2 [, 1: 5]:
cov.temp <- aggregate(dat2[, 1:5], as.yearmon, FUN = function(x) cov(x, y = dat2[index(x),"X6"])) head(cov.temp)
What gives:
> head(cov.temp) X1 X2 X3 X4 X5 Jul 1963 -0.30185387 0.09802210 0.019282934 -0.03621272 0.05332324 Aug 1963 0.14739044 0.04276340 0.081847499 -0.35195736 -0.14680017 Sep 1963 0.56698393 -0.08371676 0.003870935 -0.05948173 0.07550769 Oct 1963 0.00711595 -0.07939798 0.118030943 -0.22065278 -0.12474052 Nov 1963 0.06551982 0.22848268 0.231967655 0.02356194 -0.24272566 Dec 1963 0.23866775 0.29464398 -0.034313793 0.09694199 -0.10481527
NTN