Revision 23ed3053
Added by Benoit Parmentier over 12 years ago
climate/research/oregon/interpolation/gwr_reg.R | ||
---|---|---|
1 |
####################GWR of Tmax for 10 dates.##################### |
|
2 |
#This script generate station values for the Oregon case study. This program loads the station data from a shp file |
|
3 |
#and performs a GWR regression. |
|
4 |
#Script created by Benoit Parmentier on March 13, 2012. |
|
5 |
|
|
6 |
###Loading r library and packages |
|
7 |
library(sp) |
|
8 |
library(spdep) |
|
9 |
library(rgdal) |
|
10 |
library(spgwr) |
|
11 |
|
|
12 |
setwd("/data/computer/parmentier/Data/IPLANT_project/data_Oregon_stations/") |
|
13 |
prop<-0 |
|
14 |
|
|
15 |
###Reading the shapefile from the local directory |
|
16 |
ghcn<-readOGR(".", "ghcn_or_tmax_b_03032012_OR83M") |
|
17 |
|
|
18 |
ghcn$Northness<- cos(ghcn$ASPECT) #Adding a variable to the dataframe |
|
19 |
ghcn$Eastness <- sin(ghcn$ASPECT) #adding variable to the dataframe. |
|
20 |
|
|
21 |
ghcn$Northness_w <- sin(ghcn$slope)*cos(ghcn$ASPECT) #Adding a variable to the dataframe |
|
22 |
ghcn$Eastness_w <- sin(ghcn$slope)*sin(ghcn$ASPECT) #adding variable to the dataframe. |
|
23 |
|
|
24 |
set.seed(100) |
|
25 |
|
|
26 |
ghcn.subsets <-subset(ghcn,ghcn$date=="20100101") |
|
27 |
|
|
28 |
#summary(lm(y~x,data=df,weights=(df$wght1)^(3/4)) |
|
29 |
#ggwr.sel: find the bandwith from the data |
|
30 |
|
|
31 |
###Regression part 1: Creating a validation dataset by creating training and testing datasets |
|
32 |
|
|
33 |
n<- nrow(ghcn.subsets) |
|
34 |
ns<- n-round(n*prop) #Create a sample from the data frame with 70% of the rows |
|
35 |
nv<- n-ns #create a sample for validation with prop of the rows |
|
36 |
#ns<-n-round(n*prop) #Create a sample from the data frame with 70% of the rows |
|
37 |
ind.training <- sample(nrow(ghcn.subsets), size=ns, replace=FALSE) #This selects the index position for 70% of the rows taken randomly |
|
38 |
ind.testing <- setdiff(1:nrow(ghcn.subsets), ind.training) |
|
39 |
data_s <- ghcn.subsets[ind.training, ] |
|
40 |
data_v <- ghcn.subsets[ind.testing, ] |
|
41 |
|
|
42 |
bwG <- gwr.sel(tmax~ lon + lat + ELEV_SRTM + Eastness + Northness + DISTOC,data=data_s,gweight=gwr.Gauss, verbose = FALSE) |
|
43 |
gwrG<- gwr(tmax~ lon + lat + ELEV_SRTM + Eastness + Northness + DISTOC, data=data_s, bandwidth=bwG, gweight=gwr.Gauss, hatmatrix=TRUE) |
|
44 |
|
|
45 |
Res_fit<-gwrG$lm$residuals |
|
46 |
RMSE_f<-sqrt(sum(Res_fit^2)/ns) |
|
47 |
|
|
48 |
t<- data_s$tmax-gwrG$lm$fitted.values |
|
49 |
t2<-t-Res_fit #This should be zero |
|
50 |
|
|
51 |
#Compare the coefficients and residuals using both 30 and 100% |
|
52 |
|
|
53 |
#coefficients are stored in gwrG$SDF$lon |
|
54 |
|
|
55 |
#write out a new shapefile (including .prj component) |
|
56 |
|
|
57 |
writeOGR(data_s,".", "ghcn_1507_s", driver ="ESRI Shapefile") |
|
58 |
|
|
59 |
#ogrInfo(".", "ghcn_1507_s") #This will check the file... |
|
60 |
#plot(ghcn_1507, axes=TRUE, border="gray") |
|
61 |
|
|
62 |
#library(foreign) |
|
63 |
#dbfdata<-read.dbf("file.dbf", as.is=TRUE) |
|
64 |
##Add new attribute data (just the numbers of 1 to the numbers of objects) |
|
65 |
#dbfdata$new.att <- 1:nrow(shp) |
|
66 |
##overwrite the file with this new copy |
|
67 |
#write.dbf(dbfdata, "file.dbf") |
Also available in: Unified diff
GWR first script for one date in OR, task #364