Project

General

Profile

« Previous | Next » 

Revision a9770a19

Added by Benoit Parmentier almost 9 years ago

modifying function script to add kriging function for residuals of predictions at stations

View differences:

climate/research/oregon/interpolation/global_run_scalingup_mosaicing_function.R
4 4
#Different options to explore mosaicing are tested. This script only contains functions.
5 5
#AUTHOR: Benoit Parmentier 
6 6
#CREATED ON: 04/14/2015  
7
#MODIFIED ON: 12/04/2015            
7
#MODIFIED ON: 12/12/2015            
8 8
#Version: 1
9 9
#PROJECT: Environmental Layers project     
10 10
#COMMENTS: first commit of function script to test mosaicing using 1500x4500km and other tiles
......
1071 1071
  
1072 1072
  return(png_filename)
1073 1073
}
1074

  
1075
## functions from kriging...
1076

  
1077
fit_models<-function(list_formulas,data_training){
1078
  #This functions several models and returns model objects.
1079
  #Arguments: - list of formulas for GAM models
1080
  #           - fitting data in a data.frame or SpatialPointDataFrame
1081
  #Output: list of model objects 
1082
  list_fitted_models<-vector("list",length(list_formulas))
1083
  for (k in 1:length(list_formulas)){
1084
    formula<-list_formulas[[k]]
1085
    mod<- try(gam(formula, data=data_training)) #change to any model!!
1086
    #mod<- try(autoKrige(formula, input_data=data_s,new_data=s_sgdf,data_variogram=data_s))
1087
    model_name<-paste("mod",k,sep="")
1088
    assign(model_name,mod) 
1089
    list_fitted_models[[k]]<-mod
1090
  }
1091
  return(list_fitted_models) 
1092
}
1093

  
1094
select_var_stack <-function(r_stack,formula_mod,spdf=TRUE){
1095
  ##Write function to return only the relevant layers!!
1096
  #Note that default behaviour of the function is to remove na values in the subset 
1097
  #of raster layers and return a spdf
1098
  
1099
  ### Start
1100
  
1101
  covar_terms<-all.vars(formula_mod) #all covariates terms...+ y_var
1102
  if (length(covar_terms)==1){
1103
    r_stack_covar<-subset(r_stack,1)
1104
  } #use one layer
1105
  if (length(covar_terms)> 1){
1106
    r_stack_covar <-subset(r_stack,covar_terms[-1])
1107
  }
1108
  if (spdf==TRUE){
1109
    s_sgdf<-as(r_stack_covar,"SpatialGridDataFrame") #Conversion to spatial grid data frame, only convert the necessary layers!!
1110
    s_spdf<-as.data.frame(s_sgdf) #Note that this automatically removes all NA rows
1111
    s_spdf<-na.omit(s_spdf) #removes all rows that have na...
1112
    coords<- s_spdf[,c('s1','s2')]
1113
    coordinates(s_spdf)<-coords
1114
    proj4string(s_spdf)<-proj4string(s_sgdf)  #Need to assign coordinates...
1115
    #raster_pred <- rasterize(s_spdf,r1,"pred",fun=mean)
1116
    covar_obj<-s_spdf
1117
  } else{
1118
    covar_obj<-r_stack_covar
1119
  }
1120
  
1121
  return(covar_obj)
1122
}
1123

  
1124
remove_na_spdf<-function(col_names,d_spdf){
1125
  #Purpose: remote na items from a subset of a SpatialPointsDataFrame
1126
  x<-d_spdf
1127
  coords <-coordinates(x)
1128
  x$s1<-coords[,1]
1129
  x$s2<-coords[,2]
1130
  
1131
  x1<-x[c(col_names,"s1","s2")]
1132
  #x1$y_var <-data_training$y_var
1133
  #names(x1)
1134
  x1<-na.omit(as.data.frame(x1))
1135
  coordinates(x1)<-x1[c("s1","s2")]
1136
  proj4string(x1)<-proj4string(d_spdf)
1137
  return(x1)
1138
}
1139

  
1140
predict_auto_krige_raster_model<-function(list_formulas,r_stack,data_training,out_filename){
1141
  #This functions performs predictions on a raster grid given input models.
1142
  #Arguments: list of fitted models, raster stack of covariates
1143
  #Output: spatial grid data frame of the subset of tiles
1144
  
1145
  list_fitted_models<-vector("list",length(list_formulas))
1146
  list_rast_pred<-vector("list",length(list_formulas))
1147
  #s_sgdf<-as(r_stack,"SpatialGridDataFrame") #Conversion to spatial grid data frame, only convert the necessary layers!!
1148
  proj4string(data_training) <- projection(r_stack)
1149
  for (k in 1:length(list_formulas)){
1150
    formula_mod<-list_formulas[[k]]
1151
    raster_name<-out_filename[[k]]
1152
    #mod<- try(gam(formula, data=data_training)) #change to any model!!
1153
    s_spdf<-select_var_stack(r_stack,formula_mod,spdf=TRUE)
1154
    col_names<-all.vars(formula_mod)
1155
    if (length(col_names)==1){
1156
      data_fit <-data_training
1157
    }else{
1158
      data_fit <- remove_na_spdf(col_names,data_training)
1159
    }
1160
    
1161
    mod <- try(autoKrige(formula_mod, input_data=data_fit,new_data=s_spdf,data_variogram=data_fit))
1162
    #mod <- try(autoKrige(formula_mod, input_data=data_training,new_data=s_spdf,data_variogram=data_training))
1163
    model_name<-paste("mod",k,sep="")
1164
    assign(model_name,mod) 
1165
    
1166
    if (inherits(mod,"autoKrige")) {           #change to c("gam","autoKrige")
1167
      rpred<-mod$krige_output  #Extracting the SptialGriDataFrame from the autokrige object
1168
      y_pred<-rpred$var1.pred                  #is the order the same?
1169
      raster_pred <- rasterize(rpred,r_stack,"var1.pred",fun=mean)
1170
      names(raster_pred)<-"y_pred" 
1171
      writeRaster(raster_pred, filename=raster_name,overwrite=TRUE)  #Writing the data in a raster file format...
1172
      #print(paste("Interpolation:","mod", j ,sep=" "))
1173
      list_rast_pred[[k]]<-raster_name
1174
      mod$krige_output<-NULL
1175
      list_fitted_models[[k]]<-mod
1176
      
1177
    }
1178
    if (inherits(mod,"try-error")) {
1179
      print(paste("no autokrige model fitted:",mod,sep=" ")) #change message for any model type...
1180
      list_fitted_models[[k]]<-mod
1181
    }
1182
  }
1183
  day_prediction_obj <-list(list_fitted_models,list_rast_pred)
1184
  names(day_prediction_obj) <-c("list_fitted_models","list_rast_pred")
1185
  return(day_prediction_obj)
1186
}
1074 1187
##################### END OF SCRIPT ######################

Also available in: Unified diff