在R上季节性调整几个系列

时间:2017-02-17 14:08:23

标签: r

由于嘉年华,我建立了一个季节性调整巴西经济数据的功能。

但是这样,我可以在剪贴板中一次只调整一个系列。

然后,我一直在尝试调整更多系列(将几个系列复制到另一个系列旁边),但没有成功。

你能帮助我吗?

谢谢!

   seasbrasil<-function(y0,m0,yT,mT) {carnaval<-c(as.Date("2000-03-07"),as.Date("2001-02-27"),as.Date("2002-02-12"),as.Date("2003-03-04"),as.Date("2004-02-24"),as.Date("2005-02-08"),as.Date("2006-02-28"),as.Date("2007-02-20"),as.Date("2008-02-05"),as.Date("2009-02-24"),as.Date("2010-02-16"),as.Date("2011-03-08"),as.Date("2012-02-21"),as.Date("2013-02-12"),as.Date("2014-03-04"),as.Date("2015-02-17"),as.Date("2016-02-09"))
library(seasonal)
Sys.setenv(X13_PATH = "C:\\Users\\gfernandes\\Documents\\x13as")
checkX13()
data(holiday)
carnaval.ts <- genhol(carnaval, start = -1, end = 2, center = "calendar")
x <- read.table(file = "clipboard", sep = "\t", header=FALSE)
x <-ts(x,start=c(y0,m0),end=c(yT,mT),frequency=12)
xsa <-seas(x,xreg=carnaval.ts,regression.usertype="holiday",x11=list())
summary(xsa)
plot(xsa)
xsa<-final(xsa)
write.csv(xsa, file = "C:\\Users\\gfernandes\\Documents\\ajuste.csv")
getwd()
}

1 个答案:

答案 0 :(得分:1)

使用剪贴板读取数据不是可扩展的解决方案,而是建议     使用let c: Foo = 3; 创建文件名列表,并在此列表中应用您的功能。

list.files

<强>功能:

#Load all libraries first
library(seasonal)


#Define your data directory
DIR="C:\\path-to-your-dir\\"

#Replace .dat with  file extension applicable
# set recursive = TRUE if you have tree directory structure

TS_fileList <- list.files(path=DIR,pattern=".dat",full.names = TRUE,recursive=FALSE)



#define carnival dates
 carnaval<-c(
 "2000-03-07","2001-02-27","2002-02-12",
 "2003-03-04","2004-02-24","2005-02-08",
 "2006-02-28","2007-02-20","2008-02-05",
 "2009-02-24","2010-02-16","2011-03-08",
 "2012-02-21","2013-02-12","2014-03-04",
 "2015-02-17","2016-02-09")

#format carnival variable as date
carnaval <- as.Date(carnaval,format="%Y-%m-%d")

data(holiday)
carnaval.ts <- genhol(carnaval, start = -1, end = 2, center = "calendar")

这可能不适合您的第一次通过,但可以通过熟悉自己来解决     使用这些ATS UCLA等教程以及阅读      fn_adj_seasbrasil <-function( filePath = "C:\\path-to-your-dir\\file1.dat", carnivalTS = carnaval.ts, y0, m0, yT, mT) { #moved few operations outside this function #since they are common to all files #instead now the carnival series is #input as parameter x <- read.table(file = filePath, sep = "\t", header=FALSE) x <- ts(x,start=c(y0,m0),end=c(yT,mT),frequency=12) xsa <-seas(x,xreg = carnivalTS,regression.usertype="holiday",x11=list()) summary(xsa) plot(xsa) xsa<-final(xsa) #save seasonally adjusted file with different suffix fileName = tail(unlist(strsplit(filePath,sep="/")),1) suffix = "adjuste" #for adjusted time series of file1.dat # the name will be adjuste_file1.dat newFilePath = head(unlist(strsplit(filePath,sep="/")),1) newFileName = paste0(newFilePath,"/",suffix,"_",fileName) write.csv(xsa, file = newFileName) cat(paste0("Saved file:",newFileName,"\n")) } #define y0,m0,yT,mT and then for all files call the function lapply(TS_fileList,function(x) fn_adj_seasbrasil(filePath = x,carnivalTS = carnaval.ts, y0,m0,yT,mT) ) ?read.table?list.files等的功能帮助