I am using the eventstudies package. I am using the phys2eventtime(..)
to set up my data. However I am getting
Error in `colnames<-`(`*tmp*`, value = integer(0)) :
attempt to set 'colnames' on an object with less than two dimensions
My guess is that, my data is wrongly formated. When I have a look at the example data which use the function in the sample:
> es <- phys2eventtime(z=StockPriceReturns, events=SplitDates, width=10)
> (str(StockPriceReturns))
'zoo' series from 2000-04-03 to 2013-03-28
Data: num [1:3246, 1:30] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:30] "Bajaj.Auto" "BHEL" "Bharti.Airtel" "Cipla" ...
Index: Date[1:3246], format: "2000-04-03" "2000-04-04" "2000-04-05" "2000-04-06" ...
NULL
compared to this my data looks like that:
> (str(zoo_Data))
'zoo' series from 2002-01-01 to 2013-08-20
Data: num [1:3036] 183 183 186 191 191 ...
Index: Date[1:3036], format: "2002-01-01" "2002-01-02" "2002-01-03" "2002-01-04" ...
NULL
This is different:
- The sample data uses sth like
- attr(*, "dimnames")=List of 2
This can also be seen when looking at the raw data:
sample data:
> (head(StockPriceReturns))
Bajaj.Auto BHEL Bharti.Airtel Cipla Coal.India Dr.Reddy
2000-04-03 NA 4.9171044 NA 6.810041 NA -3.2541653
2000-04-04 NA -8.3348496 NA -3.368606 NA -8.3353739
2000-04-05 NA 0.3305788 NA 0.836825 NA 0.2616345
2000-04-06 NA -2.7605266 NA -2.466056 NA -1.8941289
2000-04-07 NA 3.2543548 NA 7.690426 NA 7.6961041
2000-04-10 NA 3.3107586 NA 6.154276 NA 6.4769648
my data:
> (head(zoo_Data))
2002-01-01 2002-01-02 2002-01-03 2002-01-04 2002-01-07 2002-01-08
182.83 182.83 186.40 190.57 191.17 193.25
The df data
:
I am constructing the zoo object out of my data like that:
> dfToZoo <- function(df) {
require(zoo)
date <- as.Date(df[, 1], format = '%d.%m.%Y')
#TODO have a look if the column are rightly named
with(df, zoo(TotalReturns, date))
}
csv_data <- read.csv(..., header = TRUE, sep = ";",stringsAsFactors=FALSE)
totalReturns <- (as.double(gsub(",",".",csv_data$TotalReturn)))
df <- data.frame(csv_data$Date, totalReturns)
names(df) <- c("Date" ,"TotalReturns")
zoo_Data <- dfToZoo(df)
How to add the attr(*, "dimnames")=List of 2
to my data?
I appreciate your replies!
UPDATE 1
This is the example data code
from the libary package:
library(eventstudies)
library(zoo)
###########################
# Load data
(data(SplitDates))
(data(StockPriceReturns))
data <- StockPriceReturns
(head(StockPriceReturns))
es <- phys2eventtime(z=StockPriceReturns, events=SplitDates, width=10)
es.w <- window(es$z.e, start=-10, end=10)
SplitDates[1:3,]
UPDATE 2
This is how my own data is formatted:
> (str(s_dates))
'data.frame': 36799 obs. of 2 variables:
$ unit: chr "ZAE000006284" "ZAE000006284" "ZAE000006284" "ZAE000006284" ...
$ when: Date, format: "2003-12-18" "2005-04-06" ...
NULL
> (str(zoo_Data))
'zoo' series from 2002-01-01 to 2013-08-20
Data: num [1:3036] 183 183 186 191 191 ...
Index: Date[1:3036], format: "2002-01-01" "2002-01-02" "2002-01-03" "2002-01-04" ...
NULL
This is how the data from the event study package is formatted:
> (str(SplitDates))
'data.frame': 22 obs. of 2 variables:
$ unit: chr "BHEL" "Bharti.Airtel" "Cipla" "Coal.India" ...
$ when: Date, format: "2011-10-03" "2009-07-24" ...
NULL
> (str(StockPriceReturns))
'zoo' series from 2000-04-03 to 2013-03-28
Data: num [1:3246, 1:30] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:30] "Bajaj.Auto" "BHEL" "Bharti.Airtel" "Cipla" ...
Index: Date[1:3246], format: "2000-04-03" "2000-04-04" "2000-04-05" "2000-04-06" ...
NULL
UPDATE 3
Reproducable example
I am initially reading the data in by different csv files. Please see my reproducable example:
Reproducable example
library(eventstudies)
library(zoo)
s_dates <-
structure(list(unit = c("ZAE000006284", "ZAE000006284", "ZAE000006284",
"ZAE000006284", "ZAE000006284", "XS0430907989", "XS0430907989"
), when = structure(c(12404, 12879, 12879, 12880, 12930, 14411,
14411), class = "Date")), .Names = c("unit", "when"), row.names = c(NA,
7L), class = "data.frame")
zoo_Data <-
structure(c(182.83, 182.83, 186.4, 190.57, 191.17, 193.25, 190.57
), index = structure(c(11688, 11689, 11690, 11691, 11694, 11695,
11696), class = "Date"), class = "zoo")
es <- phys2eventtime(z=zoo_Data, events=s_dates, width=10)
Error in `colnames<-`(`*tmp*`, value = integer(0)) :
attempt to set 'colnames' on an object with less than two dimensions
> es.w <- window(es$z.e, start=-10, end=10)
Error in window(es$z.e, start = -10, end = 10) : object 'es' not found
UPDATE 4
Ok when I try to convert my zoo_data
:
> library(eventstudies)
> library(zoo)
>
> s_dates <- dput(head(s_dates,30))
structure(list(unit = c("ZAE000006284", "ZAE000006284", "ZAE000006284",
"ZAE000006284", "ZAE000006284", "XS0430907989", "XS0430907989",
"XS0302626899", "XS0302626899", "XS0302626899", "XS0302626899",
"XS0302626899", "XS0302626899", "XS0266838746", "XS0187043079",
"XS0187043079", "XS0187043079", "XF0000TZ7757", "XF0000AK5197",
"XF0000AK5197", "XF0000AK5197", "XF0000AK5197", "XF0000AK5197",
"USU02681027", "USU026281027", "USU026281027", "USU026281027",
"USU026281027", "USU026281027", "USU026281027"), when = structure(c(12404,
12879, 12879, 12880, 12930, 14411, 14411, 14599, 14600, 15134,
15139, 15328, 15328, 13913, 14330, 14335, 14593, 13049, 12953,
12954, 12954, 12954, 12955, 12934, 13537, 13537, 13537, 13648,
13649, 13649), class = "Date")), .Names = c("unit", "when"), row.names = c(NA,
30L), class = "data.frame")
> zoo_Data <- dput(head(zoo_Data,30))
structure(c(182.83, 182.83, 186.4, 190.57, 191.17, 193.25, 190.57,
184.02, 181.34, 172.11, 169.73, 160.2, 175.09, 172.11, 170.92,
176.58, 171.51, 170.92, 173.9, 168.54, 167.34, 166.75, 166.45,
167.34, 164.37, 159.01, 158.11, 154.84, 156.63, 161.99), index = structure(c(11688,
11689, 11690, 11691, 11694, 11695, 11696, 11697, 11698, 11701,
11702, 11703, 11704, 11705, 11708, 11709, 11710, 11711, 11712,
11715, 11716, 11717, 11718, 11719, 11722, 11723, 11724, 11725,
11726, 11729), class = "Date"), class = "zoo")
>
> n = 20 ## number of observation
> cn <- unique(s_dates$unit) ## get response variable
>
> as.xts(zoo_Data)
>
> names(zoo_Data) <- cn
>
> es <- phys2eventtime(z=zoo_Data), events=s_dates, width=10)
Error in `colnames<-`(`*tmp*`, value = integer(0)) :
attempt to set 'colnames' on an object with less than two dimensions
> es.w <- window(es$z.e, start=-3, end=3)
Error in window(es$z.e, start = -3, end = 3) : object 'es' not found