Hi Joaquín.
Yes, your coordinates must be projected, in meters. Not lon lat. And obviously, every point has the same coordinates over the years; the coordinates must have the same length as the points. This looks to me like the source of the error (I've been there before :).
In my experience, working in UTM can be very cumbersome, especially if your data is spread across different grids.
My approach in these cases is to project the coordinates from degrees into a more 'general' projection. Which to choose depends on your latitude and the extent of the area. An equidistant Conic projection works well for median latitudes; another, more "local" one will work just as well. But you should centre the projection on the sample to minimise distortion, setting standard parallels 1 and 2 to the northmost and southmost coordinates of your study area. Then, if you need it, you could reproject your maps and results to UTM. This works for me.
See my code below (I sacrificed coding style for clarity). I tested it on your dataset, and it worked; it should be fine. Happy to have suggestions,
Cheers!
Susana
P.D.
(tidyverse)
dataset <- read_csv("C:/Users/SusanaRequena/Downloads/dataset.csv")
str(dataset)
dataset$lon<- as.numeric((dataset$x), rm.NA=TRUE)# change names to avoid confusions later
dataset$lat<- as.numeric((dataset$y), rm.NA=TRUE)
range(dataset$lon)
range(dataset$lat) # check there's nothing extrange
#Projecting and packing coordinates-----
library(terra)
library(sp)
centralpoint<-data.frame(cbind(mean(dataset$lon), mean(dataset$lat))) # points centroid (could be median to avoid effect of extremes)
centralpoint
# Northmost and southmost latitudes from the dataset range
lat_north <- -33.03455 # Northmost latitude
lat_south <- -33.90712 # Southmost latitude
# Convert original points to a SpatialPoints object in WGS84 (assuming it's your datum)
coords.lonlat <- SpatialPoints(cbind(dataset$lon, dataset$lat), proj4string = CRS("+proj=longlat +datum=WGS84") )
# Create your own plocal projection
DgProj <- CRS(paste("+proj=eqdc +lon_0=", centralpoint$X1, " +lat_0=", centralpoint$X2, " +lat_1=", lat_south, " +lat_2=", lat_north, sep="")) # Centrering the general projection in your study area
# Project them into your local system, derived from the Equidistant Conic in this case
coords_proj <- spTransform(coords.lonlat, DgProj)
# Add projected coordinates back into the data
dataset$X <- coordinates(coords_proj)[, 1]
dataset$Y <- coordinates(coords_proj)[, 2]
# --- Coordinates reduced to unique X,Y by point---
coords <- dataset %>%
group_by(Site) %>%
summarize(
X = mean(X), # getting rid of small variations if the GPS coords were registered in every replica
Y = mean(Y),
.groups = "drop") %>%
arrange(Site)%>%
as.data.frame() # convert tibble to data.frame
str(coords) #same lenght as points (ie. 90)
# Convert to a numeric matrix with X, Y columns
rownames(coords) <- coords$Site ## Set row names to point_id
# Now convert to matrix
coords.matrix <- as.matrix(coords[, c("X", "Y")])
# Check structure
str(coords.matrix)
class(coords.matrix)
dim(coords.matrix) # as many as points (90): 2 (X,Y)
coords.matrix ["1" ,1 ] #correct, test some of them
#6.4 Package all data into list object----
data.model <- list(y = y.dat,