2017-02-17 77 views
-1

由于嘉年华的缘故,我建立了季节性调整巴西经济数据的功能。季节性地调整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

使用剪贴板读取数据不是一个可扩展的解决方案,而不是建议 创建使用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") 

功能:

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)) 

这可能不是在第一遍你的工作,但可以通过自己熟悉 与像这些ATS UCLA教程,并阅读?read.table 功能帮助解决,?list.files?strsplit等。