Project

General

Profile

« Previous | Next » 

Revision 22886cd4

Added by Benoit Parmentier about 9 years ago

testing autokrige function for residuals of stations

View differences:

climate/research/oregon/interpolation/global_run_scalingup_mosaicing_function.R
1157 1157
    }else{
1158 1158
      data_fit <- remove_na_spdf(col_names,data_training)
1159 1159
    }
1160
    
1161
    mod <- try(autoKrige(formula_mod, input_data=data_fit,new_data=s_spdf,data_variogram=data_fit))
1160
    #use modify version of autokrige called autokrige_fun
1161
    mod <- try(autoKrige_fun(formula_mod, input_data=data_fit,new_data=s_spdf,data_variogram=data_fit))
1162 1162
    #mod <- try(autoKrige(formula_mod, input_data=data_training,new_data=s_spdf,data_variogram=data_training))
1163 1163
    model_name<-paste("mod",k,sep="")
1164 1164
    assign(model_name,mod) 
......
1184 1184
  names(day_prediction_obj) <-c("list_fitted_models","list_rast_pred")
1185 1185
  return(day_prediction_obj)
1186 1186
}
1187

  
1188
###MODIFIED AUTOKRIGE function from automap package
1189
autoKrige_fun <- function (formula, input_data, new_data, data_variogram = input_data, 
1190
    block = 0, model = c("Sph", "Exp", "Gau", "Ste"), kappa = c(0.05, 
1191
        seq(0.2, 2, 0.1), 5, 10), fix.values = c(NA, NA, NA), 
1192
    remove_duplicates = TRUE, verbose = FALSE, GLS.model = NA, 
1193
    start_vals = c(NA, NA, NA), miscFitOptions = list(), ...) 
1194
{
1195
    if (inherits(formula, "SpatialPointsDataFrame")) {
1196
        input_data = formula
1197
        formula = as.formula(paste(names(input_data)[1], "~ 1"))
1198
    }
1199
    if (!inherits(input_data, "SpatialPointsDataFrame") | !inherits(data_variogram, 
1200
        "SpatialPointsDataFrame")) {
1201
        stop(paste("\nInvalid input objects: input_data or data_variogram not of class 'SpatialPointsDataFrame'.\n\tClass input_data: '", 
1202
            class(input_data), "'", "\n\tClass data_variogram: '", 
1203
            class(data_variogram), "'", sep = ""))
1204
    }
1205
    if (as.character(formula)[3] != 1 & missing(new_data)) 
1206
        stop("If you want to use Universal Kriging, new_data needs to be specified \n  because the predictors are also required on the prediction locations.")
1207
    if (remove_duplicates) {
1208
        zd = zerodist(input_data)
1209
        if (length(zd) != 0) {
1210
            warning("Removed ", length(zd)/2, " duplicate observation(s) in input_data:", 
1211
                immediate. = TRUE)
1212
            print(input_data[c(zd), ])
1213
            input_data = input_data[-zd[, 2], ]
1214
        }
1215
    }
1216
    col_name = as.character(formula)[2]
1217
    if (length(unique(input_data[[col_name]])) == 1) 
1218
        stop(sprintf("All data in attribute '%s' is identical and equal to %s\n   Can not interpolate this data", 
1219
            col_name, unique(input_data[[col_name]])[1]))
1220
    if (missing(new_data)) 
1221
        new_data = create_new_data(input_data)
1222
    p4s_obj1 = proj4string(input_data)
1223
    p4s_obj2 = proj4string(new_data)
1224
    if (!all(is.na(c(p4s_obj1, p4s_obj2)))) {
1225
        if (is.na(p4s_obj1) & !is.na(p4s_obj2)) 
1226
            proj4string(input_data) = proj4string(new_data)
1227
        if (!is.na(p4s_obj1) & is.na(p4s_obj2)) 
1228
            proj4string(new_data) = proj4string(input_data)
1229
        #if (any(!c(is.projected(input_data), is.projected(new_data)))) 
1230
        #    stop(paste("Either input_data or new_data is in LongLat, please reproject.\n", 
1231
        #        "  input_data: ", p4s_obj1, "\n", "  new_data:   ", 
1232
        #        p4s_obj2, "\n"))
1233
        if (proj4string(input_data) != proj4string(new_data)) 
1234
            stop(paste("Projections of input_data and new_data do not match:\n", 
1235
                "  input_data: ", p4s_obj1, "\n", "  new_data:    ", 
1236
                p4s_obj2, "\n"))
1237
    }
1238
    variogram_object = autofitVariogram(formula, data_variogram, 
1239
        model = model, kappa = kappa, fix.values = fix.values, 
1240
        verbose = verbose, GLS.model = GLS.model, start_vals = start_vals, 
1241
        miscFitOptions = miscFitOptions)
1242
    krige_result = krige(formula, input_data, new_data, variogram_object$var_model, 
1243
        block = block, ...)
1244
    krige_result$var1.stdev = sqrt(krige_result$var1.var)
1245
    result = list(krige_output = krige_result, exp_var = variogram_object$exp_var, 
1246
        var_model = variogram_object$var_model, sserr = variogram_object$sserr)
1247
    class(result) = c("autoKrige", "list")
1248
    return(result)
1249
}
1187 1250
##################### END OF SCRIPT ######################

Also available in: Unified diff