2015-07-10 37 views
0

我得到了7年来气温数据分成4名季节的变量(春,夏,秋,冬),每个看起来像这样(春季例子)创建多个新的重采样系列基于观测数据

Day Month Year maxtp Season.Year Season 
1 3 2008 13.6  2008  SP 
2 3 2008 11.3  2008  SP 
3 3 2008 5.4  2008  SP 

我想(使用类似的方法来此)来创建基于这些观测数据,一个在下面的方式时多新的温度系列:Block sampling according to index in panel data

使用此代码

newseries1 <- sample(Spring, size=91, replace = T, prob = NULL) 

但是这个系列重复了91次,并不是我想要的。

我想从任意一个season.year(2008-2014)中选择一个整个Spring块,然后从任何一年中选择一个夏季块,除了之前选择的年份之外,所以除2008年以外的任何年份。重新采样年份然后被替换,以便下一次再次采样,而不是连续采样。

我想从春季变量中选取一个season.year,按照不同的季节。年份为夏季变量,然后是另一个为秋季,而另一个为冬季,并继续这样做直到重新采样是相同的观察时间长度(在这种情况下为7年)。

因此,在总结我想:

  1. 选择“块”尊重每年发生的顺序(从随机season.year春季),并开始一个新的系列,然后替换它,因此它可以是再次采样。
  2. 从一个非连续的一年跟随春天与夏天,并取而代之。
  3. 继续下去,直到重采样系列是相同的长度观察
  4. 重复这个过程,直到有100重采样系列

回答

0

对于newseries1尝试,而不是

ndays <- length(Spring[, 1]) 
#select rows of Spring randomly (are you sure you want replace = T?) 
newseries1 <- Spring[sample(1:ndays, size = ndays, replace = T, prob = NULL),] 

然后选择一年每个季节的数据依次为:

y.lst <- 2008:2014 
nssn <- 7*100*4 #desired number of annual cycles times four seasons 
y <- rep(NA, nssn) #initialise: vector of selected years 
#first spring 
y[1] <- sample(y.lst, 1) 
#subsequent seasons 
for(s in 2:nssn){ 
    #selects a year from a sublist of years which excludes that of the previous season 
    y[s] <- sample(y.lst[y.lst != y[s - 1]], 1) 
} 

然后编译所述数据帧(假设原始数据是在数据帧data):

#first Spring 
Ssn <- data[with(data, Year == y[1] & Season == "SP"),] 
ndays <- length(Spring[, 1]) 
newseries1 <- Ssn[sample(1:ndays, size = ndays, replace = T, prob = NULL),] 
#initialise data frame 
data2 <- Ssn 
#subsequent seasons 
for(s in 2:nssn){ 
    Ssn <- data[with(data, Year == y[s] & Season == "..."),] 
    ndays <- length(Spring[, 1]) 
    newseries1 <- Ssn[sample(1:ndays, size = ndays, replace = T, prob = NULL),] 
    data2 <- rbind(data2, Ssn) 
} 

你将需要创建季节标签的矢量被选择。使用余数函数在每种情况下选择适当的季节标签(即s%%4是2意味着“SU”)