2017-01-07 2 views
1

일일 견적으로 변환하려는 월별 온도가 36,000 년에 달합니다. 지금은 월 중반에 월별 추정치를 설정하고 간단한 선형 보간을 수행합니다. 이렇게하려면 래스터 :: calc 및 통계 :: approx 사용하는 시도하고 있어요이 question. 그러나 그렇게함으로써, 나는 다음과 같은 오류가 발생합니다 : 아래 큰 래스터 시리즈를 보간하여 R

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

잘하면 문제를 다시 종류의 시뮬레이션을 제공하는 몇 가지 코드입니다. 문제는 NA가 어떻게 다루어 지는지에 관한 것입니다. 왜냐하면 말단에 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) 

지금까지 아시다시피, 두 귀가 사건은 같은 길이를 가져야합니다. 다음과 같이 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) 
    } 
} 

참고 : 귀하의 코드는 매우 느린 것 같다. 한 가지 빠른 해결책은 clusterR() 함수를 사용하여 코드 속도를 높이는 것입니다. 이 기능을 사용하면 calc과 같은 일부 래스터 기능의 멀티 코어 처리를 사용할 수 있습니다. ?clusterR을 입력하거나 here을 입력하십시오.

관련 문제