gpt4 book ai didi

r - 使用 "dimnames"= List of 2 格式化动物园对象

转载 作者:行者123 更新时间:2023-12-04 17:49:33 32 4
gpt4 key购买 nike

我正在使用 eventstudies package .我正在使用 phys2eventtime(..)设置我的数据。但是我得到

Error in `colnames<-`(`*tmp*`, value = integer(0)) : 
attempt to set 'colnames' on an object with less than two dimensions

我的猜测是,我的数据格式错误。当我查看使用示例中的函数的示例数据时:
    > 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

与此相比,我的数据如下所示:
> (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

这是不同的:
  • 样本数据使用诸如 - attr(*, "dimnames")=List of 2 之类的东西

  • 在查看原始数据时也可以看到这一点:

    样本数据:
    > (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

    我的数据:
    > (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
    df data :

    我正在从我的数据中构建动物园对象,如下所示:
    > 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)

    如何添加 attr(*, "dimnames")=List of 2我的数据?

    我感谢您的回复!

    更新 1

    这是 example data code从库包:
    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,]

    更新 2

    这是我自己的数据的格式:
    > (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

    事件研究包中的数据格式如下:
    > (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

    更新 3

    可重现的示例

    我最初是通过不同的 csv 文件读取数据。请参阅我的可重现示例:

    可重现的例子
    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

    更新 4

    好的,当我尝试转换我的 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

    最佳答案

    phys2eventtime将实际日期转换为事件日期。

    因此,给定动物园时间序列和事件日期,它将实际日期转换为事件日期。这意味着,如果它在 zoo 对象的范围内找到一个事件日期,则该日期将变为 0,所有其他日期也相应地发生变化。

    所以给这个事件日期对象:

              unit       when
    1 ZAE000006284 2003-12-18
    2 ZAE000006284 2005-04-06
    3 ZAE000006284 2005-04-06
    4 ZAE000006284 2005-04-07
    5 ZAE000006284 2005-05-27
    6 XS0430907989 2009-06-16
    7 XS0430907989 2009-06-16

    转换 ts --> 事件日期的条件

    您的物理时间序列日期(动物园对象)应满足 2 个条件:
  • 中应该至少有一个列名(响应变量)单位值 (COND1)
  • 此响应变量的范围日期应至少包含一个事件日期(when 列)。(COND2)

  • 创建可转换为事件日期的物理时间序列

    现在我将创建一个满足这两个条件的时间序列:一个多时间序列(xts 对象),它的索引落在 s_dates 的范围内并且在 s_dates 单位值内具有列名。
    n = 20                       ## number of observation
    cn <- unique(s_dates$unit) ## get response variable
    rr = range(s_dates$when) ## get date range
    ## COND2
    index = seq(rr[1],rr[2],length.out=n)
    mat = matrix(round(runif(n*2,min=150,max=200),2),ncol=2)
    ev.dat = xts(mat,index)
    ## COND1
    names(ev.dat) <- cn

    检查我们的时间序列对象:
    head(ev.dat)
    ZAE000006284 XS0430907989
    2003-12-18 183.27 152.89
    2004-04-01 195.23 172.78
    2004-07-16 190.18 164.92
    2004-10-29 182.24 191.19
    2005-02-12 151.78 195.81
    2005-05-29 153.78 189.27

    毫不奇怪,它工作正常:
    (es <- phys2eventtime(z=ev.dat, events=s_dates, width=5))
    (es.w <- window(es$z.e, start=-10, end=10))
    head(es.w)
    2 3 4 5
    -4 197.28 197.28 197.28 197.28
    -3 158.54 158.54 158.54 158.54
    -2 180.56 180.56 180.56 180.56
    -1 194.41 194.41 194.41 194.41
    0 157.64 157.64 157.64 157.64
    1 151.06 151.06 151.06 151.06

    如何测试您的时间序列是否满足这两个条件:

    作为奖励,我创建了一个小函数,为您提供 2 个条件:
    check.phys2ev <- function(ev.dat,s_dates){
    is.sucess = lapply(s_dates$when,function(when){
    inter = findInterval(when, index(ev.dat))
    inter < nrow(ev.dat) && inter >1
    })
    sum(unlist(is.sucess)) >0 &
    all(colnames(ev.dat) %in% s_dates$unit)
    }

    check.phys2ev(ev.dat,s_dates)
    [1] TRUE

    关于r - 使用 "dimnames"= List of 2 格式化动物园对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20878615/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com