根据观察到的数据创建多个新的重采样系列
Creating multiple new resampled series based on observed data
我将 7 年的温度数据分为 4 个季节变量(Spring、夏季、秋季、冬季),每个季节变量如下所示(Spring 示例)
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
我想根据这些观测数据创建多个新的温度序列,一次创建一个,方法如下(使用与此类似的方法):
使用此代码
newseries1 <- sample(Spring, size=91, replace = T, prob = NULL)
但这将系列复制了 91 次,这不是我想要的。
我想 select 任何随机 season.year (2008-2014) 的整个 Spring 区块,然后 select 任何年份的夏季区块,除了年份之前选择的,所以除 2008 年以外的任何年份。然后替换重新采样的年份,以便下次可以再次重新采样,只是不是连续的。
我想从 spring 变量中取出一个 season.year,然后用一个不同的 season.year 作为夏季变量,然后是秋季,另一个是冬季,然后继续这样做,直到重新采样的长度与观察到的长度相同(在本例中为 7 年)。
总而言之,我想:
- Select一个'block'尊重年度序列(Spring来自随机season.year)并用它开始一个新系列,然后替换它这样就可以了再次采样。
- 跟随 Spring 非连续年份的夏季,并替换它。
- 继续下去,直到重新采样的系列与观察到的长度相同
- 重复此过程,直到有 100 个重采样系列
对于 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),]
然后select依次读取每个季节的年份数据:
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)
}
您需要创建要选择的季节标签矢量。在每种情况下使用 %%
余数函数 select 适当的季节标签(即 s%%4
是 2 意味着 "SU")
我将 7 年的温度数据分为 4 个季节变量(Spring、夏季、秋季、冬季),每个季节变量如下所示(Spring 示例)
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
我想根据这些观测数据创建多个新的温度序列,一次创建一个,方法如下(使用与此类似的方法):
使用此代码
newseries1 <- sample(Spring, size=91, replace = T, prob = NULL)
但这将系列复制了 91 次,这不是我想要的。
我想 select 任何随机 season.year (2008-2014) 的整个 Spring 区块,然后 select 任何年份的夏季区块,除了年份之前选择的,所以除 2008 年以外的任何年份。然后替换重新采样的年份,以便下次可以再次重新采样,只是不是连续的。
我想从 spring 变量中取出一个 season.year,然后用一个不同的 season.year 作为夏季变量,然后是秋季,另一个是冬季,然后继续这样做,直到重新采样的长度与观察到的长度相同(在本例中为 7 年)。
总而言之,我想:
- Select一个'block'尊重年度序列(Spring来自随机season.year)并用它开始一个新系列,然后替换它这样就可以了再次采样。
- 跟随 Spring 非连续年份的夏季,并替换它。
- 继续下去,直到重新采样的系列与观察到的长度相同
- 重复此过程,直到有 100 个重采样系列
对于 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),]
然后select依次读取每个季节的年份数据:
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)
}
您需要创建要选择的季节标签矢量。在每种情况下使用 %%
余数函数 select 适当的季节标签(即 s%%4
是 2 意味着 "SU")