根据观察到的数据创建多个新的重采样系列

时间:2015-07-10 09:30:36

标签: r block sample

我有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次系列,并不是我想要的。

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

我想从春季变量中获取一个季节。然后使用不同的季节。夏季变量,秋季另一个,冬季另一个,并继续这样做,直到重新采样的长度相同如观察到的(在这种情况下为7年)。

总而言之,我想:

  1. 选择一个关于年度序列的“块”(来自随机季节的春天),然后用它开始一个新系列,然后替换它以便再次采样。
  2. 从非连续一年的夏季开始关注春季,并更换它。
  3. 继续进行,直到重新采样的系列与观察到的
  4. 相同
  5. 重复此过程,直到有100个重新采样系列

1 个答案:

答案 0 :(得分:0)

$(document).ready(function() { var offset = $('.ads').offset().top, top; $(document).on('scroll', function() { top = $(window).scrollTop() < offset ? '0' : $(window).scrollTop() - offset + 'px'; $('.ads .element').css({ 'transition': 'none', 'margin-top': '-60px' }); $('.ads').css({ 'top': top }); setTimeout(function() { $('.ads .element').css({ 'transition': 'margin-top 3s', 'margin-top': '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) } 余数函数在每种情况下选择合适的季节标签(即%% 2表示“SU”)