Variogram in R with distance in km - gstat

I'm doing a spatial analysis on my data and I have their localization in terms of longitude and latitude. I calculated the sample variogram in R with geoR and gstat but what I get is the sample variogram with the distance in grades (longitude and latitude). I would like to have the same information but with the distance in km, like what happens when I calculate the spatio-temporal variogram with variogramST (gstat).
Thank you very much for those who are going to answer, it would help me a lot!

You get that with gstat if you specify for your dataset that the coordinates are degrees latitude longitude, e.g. in case you use sp, with proj4string(x) = "+proj=longlat". "distances in degrees" do not exist, they are plane wrong (except on the equator).

Related

Calculating the cosine of latitude as weights for reanalysis data in r

I am currently trying to calculate "weighted" spatial daily values for pressure using era5 data. This is due to the size of the area being represented differently towards the poles relative to lower-latitude regions. I am a little confused though. Should I multiply each value with the cosine of its latitude? So pressure * (cos(latitude)). The idea is then to apply PCA to the field. Thanks in advance!

How to code great circle distances into glmmtmb mixed effect model?

I am trying to run a mixed effect model using the 'glmmtmb' package with a spatial covariance structure that accounts for distances between points on a sphere. I have dug into the source code and identified where I think they calculate the Euclidean distances for the spatial covariance structure. I know euclidean distances are used based on this website:
https://cran.r-project.org/web/packages/glmmTMB/vignettes/covstruct.html
By bringing up the source code:
trace(getReStruc, edit = T)
Line 44 is where they use the dist(coords) for that distance matrix.
I want to change that = code so that it calculates great circle distances instead of Euclidean ones. However, functions such as distHaversine() from the 'geosphere' packages require 4 arguments (lat of x1, long of x1, lat of x2, long of x2) so I can't just plug in:
geosphere::distHaversine(coords)
Does anyone have a work around for doing this? Any help would be really appreciated!

r - DBSCAN (Density Based Clustering) describe unit of measure for eps

I was trying to use the dbscan package in R to try to cluster some spatial data. The dbscan::dbscan function takes eps and minpts as input. I have a dataframe with two columns longitude and latitude expressed in degree decimals like in the following:
df <- data.frame(lon = c(seq(1,5,1), seq(1,5,1)),
lat = c(1.1,3.1,1.2,4.1,2.1,2.2,3.2,2.4,1.4,5.1))
and I apply the algorithm:
db <- fpc::dbscan(df, eps = 1, MinPts = 2)
will eps here be defined in degrees or in some other unit ? I'm really trying to understand in which unit this maximum distance eps value is expressed so any help is appreciated
Never use the fpc package, always use dbscan::dbscan instead.
If you have latitude and longitude, you need to choose an appropriate distance function such as Haversine.
The default distance function, Euclidean, ignores the spherical nature of earth. The eps value then is a mixture of degrees latitude and longitude, but these do not correspond to uniform distances! One degree east at the equator is much farther than one degree east in Vancouver.
Even then, you need to pay attention to units. One implementation of Haversine may yield radians, another one meters, and of course someone crazy will work in miles.
Unfortunately, as far as I can tell, none of the R implementations can accelerate Haversine distance. So it may be much faster to cluster the data in ELKI instead (you need to add an index yourself though).
If your data is small enough, you can however use a precomputed distance matrix (dist object) in R. But that will take O(n²) time and memory, so it is not very scalable.

Ways to interpolate 3 dimensional data

I am working to predict an energy production using wind data. We know that power is dependent on wind direction and wind speed, and we have huge sets of data giving us the power function of wind direction and speed.
I also know how to plot those points, and have a first interpolation, but it seems the way the package interpolate is not optimal. I would also like to get the equation of the surface that is interpolated so I can test the remaining data. For now I have tried the gam() function from the mgcv package with different smooth terms, but the result is not optimal, the issue being that it seems to be a quadratic answer, whereas I would try to have polynoms of higher power.
Are there other ways to interpolate a set of 3d points ? to give you an idea of the shape of the data here's what I got from the polar representation (distance from the origin is the wind speed, angle the wind direction, z the power).
Thanks a lot !

The result by haversine formula is meter o kmeter?

I use the haversine formula to calculate the distance among the points. The result of this formula is in meter or in kmeter?
http://en.wikipedia.org/wiki/Haversine_formula
Anyone can help me?
Haversine formula is used for finding distance between to points, if latitude and longitude for both points are known.
Formula:
ACOS(SIN(Lat1)*SIN(Lat2) +COS(Lat1)*COS(Lat2)*COS(Lon2-Lon1)) *6371
Excel formula:
=ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-Lat2)) *COS(RADIANS(Long1-Long2))) *6371
Note:
Replace 6371 with 3958.756 if you want the answer in miles.
For further detail:
http://bluemm.blogspot.in/2007/01/excel-formula-to-calculate-distance.html

Resources