2017-01-07 12 views
1

私は毎日の見積もりに変換したいと思っています。今のところ、月の推定値を月の中間点に設定し、単純な線形補間を行います。これを行うには、このquestionで説明されているラスタ:: calcとstats :: approxを使用しようとしています。しかし、そうすることで、私は次のエラーを取得:以下大きいラスタシリーズを補間するR

Error in is.infinite(v) : default method not implemented for type 'list'

をうまくいけば、問題を再現するためにある種のシミュレーションを提供していくつかのコードです。私は、問題は、最後にq_interpビット(ラスタがNAに設定されていないため)が処理される方法です。つまり、私はこの情報をどうすればいいのか分かりません。

library(raster) 

#The parameters of the problem 
num_days = 9861 
months_num = 324 
num_na = 191780 

#generate baseline rasters 
r <- raster(nrows=360, ncols=720); 
values(r) <- NA 
x <- sapply(1:months_num, function(...) setValues(r, runif(ncell(r)))) 

#make them a stack 
s = stack(x) 

#define what x coordinates the rasters refer to (e.g. loosely convert monthly to daily). Probably not the most elegant solution in the world. 
num_day_month = c(31,28,31,30,31,30,31,31,30,31,30,31) 
days = as.character(seq(as.Date('1989/01/01'), as.Date('2015/12/31'), by = 'day')) 
months = as.character(seq(as.Date('1989/01/01'), as.Date('2015/12/01'), by = 'month')) 
months = substr(months, 1,nchar(months)-3) 
mid_points = as.vector(lapply(months, function(x) grep(x,days,value =T)[round(length(grep(x,days,value =T))/2)])) 
mp_loc = days %in% mid_points 
#output is the monthly mid points on the daily scale 
mp_day_locs = (1:length(days))[mp_loc] 

#make some of the cells NA throughout the whole span. In the actual dataset, the NAs generally represent oceans. 
s[sample(ncell(s), num_na)] = NA 

#a function to interpolate 
interp_row <- function(base_indexes, value_vector, return_indexes, rule_num =2) { 
    nnn = length(value_vector) 
    if (any(is.na(value_vector))) { 
    return(rep(NA, nnn)) 
    } else { 
    return(approx(x = base_indexes, y= value_vector, xout = return_indexes, rule=rule_num)$y) 
    } 
} 

#this is the function call that causes the error to be thrown 
s_interp = calc(s, function(y) interp_row(base_indexes = mp_day_locs, value_vector = y, return_indexes = 1:length(days),rule_num = 2)) 

#Now make a without NAs-- seems to work 
#generate baseline rasters 
r <- raster(nrows=360, ncols=720); 
values(r) <- NA 
x <- sapply(1:months_num, function(...) setValues(r, runif(ncell(r)))) 
#make them a stack 
q = stack(x) 
q_interp = calc(q, function(y) interp_row(base_indexes = mp_day_locs, value_vector = y, return_indexes = 1:length(days),rule_num = 2)) 

答えて

0

問題は、(私が見る限り)いずれかの値がNAである場合に生成されるリターンベクトルの長さです。 (NAなし)が正しい場合に作成されたリターン・ベクトルがあるしかし

# length = 324 
nnn = length(value_vector) 
return(rep(NA, nnn)) 

はるかに長い(毎日値):あなたのケースでは、その長さは、入力ベクトルと同じである

#length = 9861 
return(approx(x = base_indexes, y= value_vector, xout = return_indexes, rule=rule_num)$y) 

限り私が知っているように、2つのリターンケースは同じ長さを持つ必要があります。 rep(NA, length(return_indexes))を設定することで、次のようにあなたの関数を変更しよう:

interp_row <- function(base_indexes, value_vector, return_indexes, rule_num =2) { 
    nnn = length(value_vector) 
    if (any(is.na(value_vector))) { 
    return(rep(NA, length(return_indexes))) 
    } else { 
    return(approx(x = base_indexes, y= value_vector, xout = return_indexes, rule=rule_num)$y) 
    } 
} 

注:あなたのコードは非常に遅いと思われます。便利な解決策の1つは、clusterR()関数を使用してコードをスピードアップすることです。この機能は、calcのようないくつかのラスタ関数のマルチコア処理の使用を可能にします。 ?clusterRを入力するか、見てhereとしてください。

関連する問題