diff --git a/R/Intlr.R b/R/Intlr.R index a1a8f76ec89f32849e2c980265d69d0ae8b62c99..f6d972171b14b152b5adfaaea049ef779175a6e0 100644 --- a/R/Intlr.R +++ b/R/Intlr.R @@ -504,13 +504,16 @@ Intlr <- function(exp, obs, exp_cor = NULL, exp_lats, exp_lons, obs_lats, obs_lo if (!is.null(exp_cor)) { aux_dim <- NULL forecast <- exp_cor_interpolated$data - target_dims_predictor <- c(sdate_dim, member_dim) - target_dims_forecast <- c(sdate_dim, member_dim) + if (dim(predictor)[[member_dim]] == dim(forecast)[[member_dim]]) { + target_dims_forecast <- c(sdate_dim) + } else { + target_dims_predictor <- c(sdate_dim, member_dim) + target_dims_forecast <- c(sdate_dim, member_dim) + } } else { forecast <- NULL target_dims_forecast <- NULL } - } # (Multi) linear regression with large-scale predictors @@ -534,8 +537,12 @@ Intlr <- function(exp, obs, exp_cor = NULL, exp_lats, exp_lons, obs_lats, obs_lo } else { forecast <- exp_cor$data } - target_dims_predictor <- c(sdate_dim, large_scale_predictor_dimname, member_dim) - target_dims_forecast <- c(sdate_dim, large_scale_predictor_dimname, member_dim) + if (dim(predictor)[[member_dim]] == dim(forecast)[[member_dim]]) { + target_dims_forecast <- c(sdate_dim, large_scale_predictor_dimname) + } else { + target_dims_predictor <- c(sdate_dim, large_scale_predictor_dimname, member_dim) + target_dims_forecast <- c(sdate_dim, large_scale_predictor_dimname, member_dim) + } } } @@ -597,15 +604,18 @@ Intlr <- function(exp, obs, exp_cor = NULL, exp_lats, exp_lons, obs_lats, obs_lo } target_dims_predictand <- sdate_dim + target_dims_predictor <- c(sdate_dim,'nn') if (!is.null(exp_cor)) { - target_dims_predictor <- c(sdate_dim,'nn', member_dim) - target_dims_forecast <- c(sdate_dim,'nn', member_dim) - } else { - target_dims_predictor <- c(sdate_dim,'nn') - } - } else { + if (dim(predictor)[[member_dim]] == dim(forecast)[[member_dim]]) { + target_dims_forecast <- c(sdate_dim,'nn') + } else { + target_dims_predictor <- c(sdate_dim,'nn', member_dim) + target_dims_forecast <- c(sdate_dim,'nn', member_dim) + } + } + } else { stop(paste0(lr_method, " method is not implemented yet")) } @@ -686,7 +696,7 @@ Intlr <- function(exp, obs, exp_cor = NULL, exp_lats, exp_lons, obs_lats, obs_lo require (plyr) - if (!is.null(f)) { + if (!is.null(f) & any(names(dim(f)) == member_dim)) { if (!is.null(aux_dim)) { tmp_df <- data.frame(x = adply(x,.margins = 3, .id = NULL, .fun = as.matrix), y = y) } else { @@ -725,7 +735,7 @@ Intlr <- function(exp, obs, exp_cor = NULL, exp_lats, exp_lons, obs_lats, obs_lo res <- .pred_lm(lm1 = lm1, df = tmp_df, f = f, loocv = loocv, aux_dim = aux_dim, k_out = k_out, pca = pca) } - return(res) + return(res) } #----------------------------------- @@ -819,9 +829,13 @@ Intlr <- function(exp, obs, exp_cor = NULL, exp_lats, exp_lons, obs_lats, obs_lo } else { if (!is.null(aux_dim)) { # 9nn & large-scale - # concatenate members - fcst_df <- as.data.frame(matrix(aperm(f, c(1, 3, 2)), - nrow = dim(f)[1] * dim(f)[3], ncol = dim(f)[2])) + if (length(dim(f)) == 3) { + # if ens member number is different in hcst and fcst, concatenate members + fcst_df <- as.data.frame(matrix(aperm(f, c(1, 3, 2)), + nrow = dim(f)[1] * dim(f)[3], ncol = dim(f)[2])) + } else { + fcst_df <- as.data.frame(f) + } if (pca) { ## transfer test data to PCA space. fcst_df <- as.matrix(fcst_df)%*%lm1$rotation