计算 stl() 的开始

发布于 2024-10-07 23:55:14 字数 767 浏览 0 评论 0原文

我正在从 .csv 数据文件中读取每周数据。数据示例是:

Date,Demand    
"Feb 08, 1991",6621    
"Feb 15, 1991",6433    
"Feb 22, 1991",6582   
"Mar 01, 1991",7224   
"Mar 08, 1991",6875   
"Mar 15, 1991",6947   
"Mar 22, 1991",7328   
"Mar 29, 1991",6777   
"Apr 05, 1991",7503
.....  

我的代码是:

> temp<-read.table(file="E:\\Data\\Demand_00.csv",header=TRUE, sep=",")
> stadat<-strptime(as.character(temp[,1]),"%b %d, %Y")[1]
> statim<-as.numeric(strftime(stadat,"%Y"))+(as.numeric(strftime(stadat,"%j"))/366)
> temdat<-ts(temp[,2],start=statim,frequency=52)
> plot(temp2<- stl(log(temdat), "per"))

我的问题是:是否有更好/更干净的方法来构建 statim(上述 ts 对象中所需的启动)?请注意,这是每周数据,可能会也可能不会从一年的第一周开始。

谢谢,
账单

I'm reading weekly data from a .csv data file. A sample of the data is:

Date,Demand    
"Feb 08, 1991",6621    
"Feb 15, 1991",6433    
"Feb 22, 1991",6582   
"Mar 01, 1991",7224   
"Mar 08, 1991",6875   
"Mar 15, 1991",6947   
"Mar 22, 1991",7328   
"Mar 29, 1991",6777   
"Apr 05, 1991",7503
.....  

My code is:

> temp<-read.table(file="E:\\Data\\Demand_00.csv",header=TRUE, sep=",")
> stadat<-strptime(as.character(temp[,1]),"%b %d, %Y")[1]
> statim<-as.numeric(strftime(stadat,"%Y"))+(as.numeric(strftime(stadat,"%j"))/366)
> temdat<-ts(temp[,2],start=statim,frequency=52)
> plot(temp2<- stl(log(temdat), "per"))

My question is: Is there a better/cleaner way to build statim (the start required in the above ts object)? Notice that this is weekly data that may or may not start at the first week of the year.

Thanks,
Bill

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2024-10-14 23:55:15

您可以使用 Zoo 软件包来简化此操作:

File <- E:\\Data\\Demand_00.csv"

library(zoo)
fmt <- "%b %d, %Y"

year.jul <- function(x) as.numeric(format(x, "%Y")) + 
    as.numeric(format(x, "%j"))  / 366
z0 <- read.zoo(File, header = TRUE, sep = ",", FUN = as.Date, format = fmt,
    FUN2 = year.jul)
ts(z0, start = start(z0), frequency = 52)

另一方面,您可能希望在 Epi 软件包中使用 cal.yr ,而不是将其强制为 366 天:

library(Epi)
z2 <- read.zoo(File, header = TRUE, sep = ",", FUN = cal.yr, format = fmt)
as.ts(z2)

You could use the zoo package to simplify this:

File <- E:\\Data\\Demand_00.csv"

library(zoo)
fmt <- "%b %d, %Y"

year.jul <- function(x) as.numeric(format(x, "%Y")) + 
    as.numeric(format(x, "%j"))  / 366
z0 <- read.zoo(File, header = TRUE, sep = ",", FUN = as.Date, format = fmt,
    FUN2 = year.jul)
ts(z0, start = start(z0), frequency = 52)

On the other hand rather than forcing it into 366 days you might want to use cal.yr in the Epi package:

library(Epi)
z2 <- read.zoo(File, header = TRUE, sep = ",", FUN = cal.yr, format = fmt)
as.ts(z2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文