Maxent Species Distribution Modelling in R: Error in data.frame(..., check.names = FALSE)
This is a question posted in biomod2 (https://github.com/biomodhub/biomod2/issues/539), the original author seems to be looking in the wrong place, I had the same problem today so I'm reproducing it here.
Issue:
I have two dataframes with 15325 rows, and they have no NAs:
dat_Winter: contains environmental variables dolphin_coords_xy: contains longitude and latitude positions in decimal degrees
I am trying to run a Maxent model using the function maxent() from the dismo package. Whenever I run the maxent model, I keep on getting this error message (below).
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 1, 2, 0
This message means that I'm trying to create a data.frame from vectors or columns that have a mismatched number of rows. However, both data frames have the same number of rows.
I don't understand why I'm getting this error message as I've met the requirements of 'x' and 'p', I have no NA's, I don't have any duplicates, the rows aren't mismatched in theory. Am I missing something?
I can't share the data due to ownership issues but I can provide a copy of the first 5 rows to show what my data looks like and some dummy data.
#Select the presence points from the dataframe 'dolphin_coords1" dolphin_coords_xy <- dolphin_coords1 %>% dplyr::select(1, 2) dolphin_coords_xy<-as.data.frame(dolphin_coords_xy) p=dolphin_coords_xy
#make model max1_Winter <- maxent(x = dat_Winter, p = dolphin_coords_xy)
#------------Do some troubleshooting--------------------------
#Check that the rows are of equal length nrow(dat_Winter) #15325 rows nrow(dolphin_coords_xy) #15325 rows
Check for NA values in columns sum(is.na(dolphin_coords_xy$x)) # Count NA values (NAs = 0) sum(is.na(dolphin_coords_xy$y)) # Count NA values (NAs = 0)
#Check the class of the two data frames class(dat_Winter) #data.frame class(dolphin_coords_xy) #dataframe
#Check Requirements x = Predictors can either be a Raster* object or SpatialGridDataFrame or a dataframe, in which each column should be a predictor variable and each row a presence or background record
p = If p is a data.frame or matrix it represents a set of point locations; and it must have two columns with the first being the x-coordinate (longitude) and the second the y-coordinate (latitude). Dataframe 1 dat_Winter:
SST Chlor.a Slope Distance Depth
1 26.03596 0.2764387 3.12927913 0.0000 5 2 25.84813 0.3505107 0.54305966 412.0501 9 3 25.79146 0.3326796 0.96003758 4175.7593 62 4 26.03121 0.2818667 2.14737457 412.5744 5 5 26.03596 0.2764387 3.12927913 0.0000 5
Dataframe 2 coords_dolphins_xy:
x y
1 33.89083 27.26778 2 33.86782 27.40854 3 33.86230 27.44623 4 33.88653 27.26957 5 33.88766 27.26848
Dummy data:
structure(list(x = c(33.7250201948918, 33.0372212855145, 33.1162582356483, 33.1780943416525, 33.325465941336, 33.1426498885266, 33.372344966745, 33.7141977476422, 33.5114065359812, 33.4723861014936), y = c(28.0621412244625, 27.6205332666635, 28.0223171819933, 27.5313555219211, 27.1894561205059, 27.5307895665988, 28.1612804006785, 27.5815055153333, 27.3029501273297, 27.3116279762238), Calf_Presence = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), SSS = c(1027.47942044867, 1026.32734772185, 1027.6889518555, 1026.61821882308, 1026.73625519967, 1026.99284255489, 1026.50956621722, 1026.42982337421, 1026.87068500051, 1026.71667900492), SST = c(25.0228688765572, 27.6068648373514, 23.5967889237256, 26.6370548686025, 26.2696984540073, 24.8069121214026, 27.1928669289746, 27.3427573793279, 26.2358166318412, 26.8545404103531), Chlor.a = c(0.234918360422555, 0.552972766326481, 0.372645556572673, 0.158839964433605, 0.155691480776937, 0.322592780862786, 0.16285265978558, 0.155731527084381, 0.207670826649629, 0.174525638280348 ), Slope = c(4.50732010781271, 0.262136190366096, 0.164475474050542, 0.725271958307515, 0.793811857372093, 0.0233933381961778, 2.86868026925737, 2.04465639865241, 1.15032474698361, 0.930412189550645), Distance = c(102293.714232872, 332276.294160756, 126047.003446403, 146918.398130579, 158796.57696904, 27015.5468157929, 190039.211871486, 265043.121310163, 105894.776220919, 98221.3073956403), Depth = c(27, 2, 52, 810, 1081, 76, 819, 879, 705, 738)), row.names = c(NA, 10L), class = "data.frame")Please make sure to close the issue once you consider it as solved
On some other pages it was suggested that this thing might be caused by the default value of removeDuplicates being true, I tried removeDuplicates=FALSE and that didn't solve the problem for me though!