0
> library(gstat)
> library(readxl)
> library(sp)
> library(rgdal)

#2013(FATALITIES)

data <- read.csv("C:/Users/sbs/Desktop/Study/RA/Accident analysis/New folder/Iowa total crashes 2013_cleaned.csv", header = TRUE, sep =",")
plot(data)
attach(data)

> class(data)

[1] "data.frame"

> coordinates(data) <- ~XCOORD+YCOORD

> class(data)

[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

> variogram1<- variogram(FATALITIES~1, data)
> plot(variogram1)

> fit.variogram1<- fit.variogram(variogram1, vgm(0.002, "Exp"))

> plot(variogram1, fit.variogram1, cutoff = 300000, pch =20, col ="red")

> fit.variogram1

  model       psill    range
1   Exp 0.007896732 13527.15

Until this point everything looks good. My "data" is in CVS format, units meters. Fitted variogram model looks good.

I have made "X" my prediction surface or grid using ArcGIS having 500*500 meter as cell size of fishnet. It is a shapefile.

> x<- readOGR("H:/p/Road_Network_Info/road_fish_lable.shp")

OGR data source with driver: ESRI Shapefile 
Source: "H:\p\Road_Network_Info\road_fish_lable.shp", layer: "road_fish_lable"
with 5006 features
It has 1 fields
Integer64 fields read as strings:  Id 

> plot(x)

> class(x)

[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

Everything good until now. When I start performing kriging, this error shows up. But the plots of my "data" and Grid (X) overlap each other perfectly.

> krige(FATALITIES~1, data, x, fit.variogram1)

[1] NA
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
Error in predict.gstat(g, newdata = newdata, block = block, nsim = nsim,  : 
  var1 : data item in gstat object and newdata have different coordinate reference systems

I am performing analysis on fatalities in state of Iowa, USA. Here, both X and data are in meters with same coordinates. i have checked it twice. I am using NAD 1987 Coordinates in ArcGIS to make the required shapefile.

1

The proj string for your newdata appears to be coordinate reference system 4326, which is in units degrees. If your data is, as you say, in units meters, then they are indeed not in identical coordinate reference systems. Try reprojecting your data to that string returned in the error first and see if that solves the problem:

library(rgdal)

dataReprojected <- spTransform(data, CRS=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

then carry on with your analysis, using dataReprojected.

The error also seems to say the your data has no CRS (it returns two values, one is NA the other a proj string, so maybe you just need to set the CRS of your data in the first place. If your data is in units meters then I don't think that other proj string is correct for your data: find out the CRS of your data and set it...

  • That was really helpful. but now its showing a different error. [using ordinary kriging] Error in predict.gstat(g, newdata = newdata, block = block, nsim = nsim, : out of dynamic memory (try local kriging?) – Sudhanshu Tyagi Jun 21 at 20:52
  • set maxdist and optionally nmin and nmax to do local kriging; that will limit the number of points that go into the kriging matrices so that you don't run out of memory. maxdist is in the units of your coordinate reference system--eg meters or degrees. If nmax is set, then the number of points used to krige will also be limited to that value. – 0mn1 Jun 23 at 4:22
  • yes, i got the results but they are not as expected. I am using spplot(data.kringed["var1.var"]) to view my data, 'data.kringed' is name of the data which i am trying to plot. But the plot which is generated has no variation, throughout the plot i am having same value, which is 0.1265(no variotion). which should not be the case. There should be some hotspots where more fatalities have occurred. – Sudhanshu Tyagi Jul 2 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.