Kristi Glover
2013-May-16 21:48 UTC
[R] Why is it not working the MaxEnt analysis (package "maxent", and "dismo")?
Hi R-user, I was trying to compute probability of species distribution using MaxEnt. the MaxEnt uses only teh sites in which species present, I tried to run but did not work, would you give me some hints? I have given an example table and how I tried it. --------------------------------------------------- install.packages("maxent", dependencies=TRUE) install.packages("dismo", dependencies=TRUE) library(dismo) library(maxent) dd<-structure(list(long = c(-75.747, -106.84, -105.27, -104.64, -103.71, -102.72, -101.79, -100.8, -105.69, -104.67, -103.71, -102.69, -101.66, -100.71, -99.685, -98.656, -97.627, -111.82, -110.2, -109.55, -106.88, -106.84, -105.85, -104.82, -103.84, -102.8, -101.83, -100.79, -99.742, -98.695, -97.744, -112.46, -111.8, -110.79, -108.41, -107.74), lat = c(19.792, 21.576, 21.347, 21.243, 21.078, 21.185, 20.99, 21.067, 22.621, 22.763, 22.594, 22.704, 22.797, 22.581, 22.642, 22.685, 22.711, 24.153, 23.44, 23.385, 23.4, 23.702, 23.565, 23.71, 23.542, 23.654, 23.454, 23.533, 23.596, 23.64, 23.377, 24.796, 24.459, 24.399, 24.503, 24.429 ), sp1 = c(0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L), sp2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), sp3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), sp4 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), env1 = c(182.0037689, 163.3301239, 443.0214233, 1240.155273, 1774.867432, 1909.528809, 2153.244141, 1878.018433, 909.1315308, 1980.898438, 2271.118896, 2122.672852, 2033.41626, 1534.658447, 828.4759522, 222.3117523, 23.4761219, 79.52642822, 322.994751, 273.6637268, 35.5085907, 516.3795776, 2205.419434, 2118.727539, 2178.901123, 1995.210083, 2048.075928, 1824.84021, 1043.509888, 184.4526062, 12.6928978, 20.87172508, 126.5344544, 258.8852844, 3.28964257, 140.3287811 ), env2 = c(1134L, 550L, 2111L, 2523L, 2156L, 1209L, 1107L, 2605L, 3176L, 2490L, 1360L, 801L, 1118L, 1484L, 2730L, 1309L, 104L, 197L, 2033L, 1339L, 567L, 2694L, 2708L, 1806L, 1344L, 912L, 1377L, 2346L, 3265L, 989L, 69L, 428L, 764L, 896L, 100L, 1521L), env3 = c(24.533, 24.928, 24.707, 21.052, 21.318, 18.428, 19.041, 17.743, 24.371, 19.689, 16.879, 16.528, 16.901, 18.015, 20.648, 25.31, 24.308, 22.528, 22.912, 22.001, 25.097, 25.391, 19.154, 14.943, 17.143, 16.898, 16.891, 17.563, 15.63, 24.354, 24.088, 22.527, 22.528, 22.126, 25.317, 25.643), env4 = c(0.047969, 0.003469, 0.003385, 0.002253, 0.000791, 0.001834, 0.008016, 0.009262, 0.003934, 0.002322, 0.00061, 0.000799, 6.4e-05, 0, 0, 0.000107, 0.003151, 0.018915, 0.015077, 0.004554, 0.003499, 0.002705, 0.003507, 0.001173, 0.000149, 0.000308, 0, 0, 0, 0.00074, 0.002845, 0.017047, 0.018915, 0.017111, 0.002417, 0.002668)), .Names = c("long", "lat", "sp1", "sp2", "sp3", "sp4", "env1", "env2", "env3", "env4"), class = "data.frame", row.names = c(NA, -36L)) predi<-dd[,7:10] spA<-subset(dd,dd$sp1==1) m1 <- maxent(spA[,7:10], spA[,'sp1']) # for species 1 m1.prediction<-predict(m1, predi) spB<-subset(dd,dd$sp2==1) m2 <- maxent(spB[,7:10], spB[,'sp2']) # for species 2 m2.prediction<-predict(m2, predi) spC<-subset(dd,dd$sp3==1) m3 <- maxent(spC,7:10], spC[,'sp3']) # for species 3 m3.prediction<-predict(m3, predi) spD<-subset(dd,dd$sp4==1) m4 <- maxent(spD[,7:10], spD[,'sp4']) # for species 4 m4.prediction<-predict(m4, predi) prediction<-cbind(m1.prediction,m2.prediction,m3.prediction,m4.prediction) But, it did not work. I could not figure it out why it is not working. Another thing, How I can make a loop for several species? cheers, KG [[alternative HTML version deleted]]
Kristi Glover
2013-May-17 02:38 UTC
[R] FW: Why is it not working the MaxEnt analysis (package "maxent", and "dismo")?
Hi Timothy, I was trying to get a probability of the species based on MaxEnt function. As per the theory of MaxEnt,I uses the sites where only species is present. Therefore I selected only those rows where the species is present, my data is : 'dd', in which- there are X,Y coordinates and 4 different kind of species (sp1, sp2, sp3, sp4) and four environmental variables (env1-env4). and trying to develop model using the sites in which the species present, For example, sp1: to select the sites in which the species is present were selected spA<-subset(dd,dd$sp1==1)# select the rows in which species 1 is present After that, I was trying to develop a model for the species, using following m1 <- maxent(spA[,7:10], spA[,'sp1']) # develop a model for species 1 based on the data where the species is present After that, I wanted to use this model to predict using new data (new data set- that is 'predi'). but, when I run but it R crashed. m1.prediction<-predict(m1, predi) # apply the model for new data Dennis also tried to help me but his R was also crashed. Any thing- did I mistake from which our Rs were crashed? Any idea? thanks KG -------------------------------------------------------------------------------------------------------- install.packages("maxent", dependencies=TRUE) library(maxent) dd<-structure(list(long = c(-75.747, -106.84, -105.27, -104.64, -103.71, -102.72, -101.79, -100.8, -105.69, -104.67, -103.71, -102.69, -101.66, -100.71, -99.685, -98.656, -97.627, -111.82, -110.2, -109.55, -106.88, -106.84, -105.85, -104.82, -103.84, -102.8, -101.83, -100.79, -99.742, -98.695, -97.744, -112.46, -111.8, -110.79, -108.41, -107.74), lat = c(19.792, 21.576, 21.347, 21.243, 21.078, 21.185, 20.99, 21.067, 22.621, 22.763, 22.594, 22.704, 22.797, 22.581, 22.642, 22.685, 22.711, 24.153, 23.44, 23.385, 23.4, 23.702, 23.565, 23.71, 23.542, 23.654, 23.454, 23.533, 23.596, 23.64, 23.377, 24.796, 24.459, 24.399, 24.503, 24.429 ), sp1 = c(0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L), sp2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), sp3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), sp4 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), env1 = c(182.0037689, 163.3301239, 443.0214233, 1240.155273, 1774.867432, 1909.528809, 2153.244141, 1878.018433, 909.1315308, 1980.898438, 2271.118896, 2122.672852, 2033.41626, 1534.658447, 828.4759522, 222.3117523, 23.4761219, 79.52642822, 322.994751, 273.6637268, 35.5085907, 516.3795776, 2205.419434, 2118.727539, 2178.901123, 1995.210083, 2048.075928, 1824.84021, 1043.509888, 184.4526062, 12.6928978, 20.87172508, 126.5344544, 258.8852844, 3.28964257, 140.3287811 ), env2 = c(1134L, 550L, 2111L, 2523L, 2156L, 1209L, 1107L, 2605L, 3176L, 2490L, 1360L, 801L, 1118L, 1484L, 2730L, 1309L, 104L, 197L, 2033L, 1339L, 567L, 2694L, 2708L, 1806L, 1344L, 912L, 1377L, 2346L, 3265L, 989L, 69L, 428L, 764L, 896L, 100L, 1521L), env3 = c(24.533, 24.928, 24.707, 21.052, 21.318, 18.428, 19.041, 17.743, 24.371, 19.689, 16.879, 16.528, 16.901, 18.015, 20.648, 25.31, 24.308, 22.528, 22.912, 22.001, 25.097, 25.391, 19.154, 14.943, 17.143, 16.898, 16.891, 17.563, 15.63, 24.354, 24.088, 22.527, 22.528, 22.126, 25.317, 25.643), env4 = c(0.047969, 0.003469, 0.003385, 0.002253, 0.000791, 0.001834, 0.008016, 0.009262, 0.003934, 0.002322, 0.00061, 0.000799, 6.4e-05, 0, 0, 0.000107, 0.003151, 0.018915, 0.015077, 0.004554, 0.003499, 0.002705, 0.003507, 0.001173, 0.000149, 0.000308, 0, 0, 0, 0.00074, 0.002845, 0.017047, 0.018915, 0.017111, 0.002417, 0.002668)), .Names = c("long", "lat", "sp1", "sp2", "sp3", "sp4", "env1", "env2", "env3", "env4"), class = "data.frame", row.names = c(NA, -36L)) predi<-dd[,7:10] spA<-subset(dd,dd$sp1==1) m1 <- maxent(spA[,7:10], spA[,'sp1']) # for species 1 m1.prediction<-predict(m1, predi)# predict species 1 using new data set spB<-subset(dd,dd$sp2==1) m2 <- maxent(spB[,7:10], spB[,'sp2']) # for species 2 m2.prediction<-predict(m2, predi) spC<-subset(dd,dd$sp3==1) m3 <- maxent(spC,7:10], spC[,'sp3']) # for species 3 m3.prediction<-predict(m3, predi) spD<-subset(dd,dd$sp4==1) m4 <- maxent(spD[,7:10], spD[,'sp4']) # for species 4 m4.prediction<-predict(m4, predi) prediction<-cbind(m1.prediction,m2.prediction,m3.prediction,m4.prediction) -----------------------------------------------------------------------------------------------------------> Date: Thu, 16 May 2013 17:53:56 -0700 > Subject: Re: [R] Why is it not working the MaxEnt analysis (package "maxent", and "dismo")? > From: djmuser@gmail.com > To: kristi.glover@hotmail.com > > Hi: > > R crashed when I tried to run your first set of predictions, and it > doesn't really surprise me. > > * predi consists of the variables env1-env4 > > AFAICT, this code: > spA<-subset(dd,dd$sp1==1) > m1 <- maxent(spA[,7:10], spA[,'sp1']) # for species 1 > m1.prediction<-predict(m1, predi) > > attempts to select out the rows of dd for which sp1 == 1. Then you run > the maxent function on the rows of predi for which sp1 == 1, where the > second argument is 1. If you read the help page for maxent::maxent, > you'll discover that the first argument is OK, but the second argument > expects > "A factor or vector of labels corresponding to each document in the > feature_matrix" > Your second argument is a numeric constant. > > What I didn't understand was why you would want to use predi as the > set of values at which to get model predictions when a subset of that > matrix was used as the response in the maxent() call that returns the > fitted model. > > I don't know enough about what this package does to provide a solid > rationale for what is right or wrong with your code, but I do know > that whatever you did is 'bad' in the sense that it was sufficient to > crash R. This often means some underlying C code threw a segmentation > fault as a result of your inputs. If I had to guess, I would posit > that the second argument is meant to be a (variable) response and you > fed it a constant vector of 1's. What havoc that creates in the > package code is beyond my understanding. > > I don't know if you got a crash on your end, but it's not something > that should happen so you should contact the maintainer of the maxent > package and report the problem, including the data and code that > induced the crash. > > maintainer("maxent") > [1] "Timothy P. Jurka <tpjurka@ucdavis.edu>" > > He'll undoubtedly have a better idea of the source of the problem than > me. I got it from the first block of code above using > > > sessionInfo() > R version 3.0.0 (2013-04-03) > Platform: x86_64-w64-mingw32/x64 (64-bit) > > locale: > [1] LC_COLLATE=English_United States.1252 > [2] LC_CTYPE=English_United States.1252 > [3] LC_MONETARY=English_United States.1252 > [4] LC_NUMERIC=C > [5] LC_TIME=English_United States.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] maxent_1.3.3 tm_0.5-8.3 SparseM_0.99 Rcpp_0.10.3 dismo_0.8-11 > [6] raster_2.1-25 sp_1.0-9 > > loaded via a namespace (and not attached): > [1] grid_3.0.0 lattice_0.20-15 slam_0.1-28 tools_3.0.0 > > in a fresh R session. > > > > Aside: I attempted to write a function that would cut down the amount > of typing that you would need to do, something like > > predFun <- function(DF, var, ...) > { > require(maxent) > s1 <- DF[DF[[var]] == 1, ] > m <- maxent(s1[ , 7:10], s1[, var]) > predict(m, predi) > } > > ## Note: subset() uses nonstandard evaluation, so it is not recommended > ## for use inside of function bodies. > > ## Note 2: Whatever code fixes are recommended, make them in the function. > > vars <- paste0("sp", 1:4) > > # Run predFun recursively on each element of vars, > # which becomes the x in the anonymous function > # comprising the second argument of lapply() > > L <- lapply(vars, function(x) predFun(DF, x)) > do.call(cbind, L) # cbind the results > > Obviously untested, but it gives you an idea of how the game works. > > Dennis > > On Thu, May 16, 2013 at 2:48 PM, Kristi Glover > <kristi.glover@hotmail.com> wrote: > > Hi R-user, > > I was trying to compute probability of species distribution using MaxEnt. the MaxEnt uses only teh sites in which species present, > > I tried to run but did not work, would you give me some hints? > > > > I have given an example table and how I tried it. > > --------------------------------------------------- > > install.packages("maxent", dependencies=TRUE) > > install.packages("dismo", dependencies=TRUE) > > library(dismo) > > library(maxent) > > > > dd<-structure(list(long = c(-75.747, -106.84, -105.27, -104.64, -103.71, > > -102.72, -101.79, -100.8, -105.69, -104.67, -103.71, -102.69, > > -101.66, -100.71, -99.685, -98.656, -97.627, -111.82, -110.2, > > -109.55, -106.88, -106.84, -105.85, -104.82, -103.84, -102.8, > > -101.83, -100.79, -99.742, -98.695, -97.744, -112.46, -111.8, > > -110.79, -108.41, -107.74), lat = c(19.792, 21.576, 21.347, 21.243, > > 21.078, 21.185, 20.99, 21.067, 22.621, 22.763, 22.594, 22.704, > > 22.797, 22.581, 22.642, 22.685, 22.711, 24.153, 23.44, 23.385, > > 23.4, 23.702, 23.565, 23.71, 23.542, 23.654, 23.454, 23.533, > > 23.596, 23.64, 23.377, 24.796, 24.459, 24.399, 24.503, 24.429 > > ), sp1 = c(0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, > > 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, > > 0L, 0L, 1L, 0L, 1L, 1L, 0L), sp2 = c(0L, 0L, 0L, 0L, 0L, 0L, > > 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > > 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), sp3 = c(0L, > > 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > > 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > > 0L, 0L, 0L), sp4 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, > > 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, > > 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), env1 = c(182.0037689, > > 163.3301239, 443.0214233, 1240.155273, 1774.867432, 1909.528809, > > 2153.244141, 1878.018433, 909.1315308, 1980.898438, 2271.118896, > > 2122.672852, 2033.41626, 1534.658447, 828.4759522, 222.3117523, > > 23.4761219, 79.52642822, 322.994751, 273.6637268, 35.5085907, > > 516.3795776, 2205.419434, 2118.727539, 2178.901123, 1995.210083, > > 2048.075928, 1824.84021, 1043.509888, 184.4526062, 12.6928978, > > 20.87172508, 126.5344544, 258.8852844, 3.28964257, 140.3287811 > > ), env2 = c(1134L, 550L, 2111L, 2523L, 2156L, 1209L, 1107L, 2605L, > > 3176L, 2490L, 1360L, 801L, 1118L, 1484L, 2730L, 1309L, 104L, > > 197L, 2033L, 1339L, 567L, 2694L, 2708L, 1806L, 1344L, 912L, 1377L, > > 2346L, 3265L, 989L, 69L, 428L, 764L, 896L, 100L, 1521L), env3 = c(24.533, > > 24.928, 24.707, 21.052, 21.318, 18.428, 19.041, 17.743, 24.371, > > 19.689, 16.879, 16.528, 16.901, 18.015, 20.648, 25.31, 24.308, > > 22.528, 22.912, 22.001, 25.097, 25.391, 19.154, 14.943, 17.143, > > 16.898, 16.891, 17.563, 15.63, 24.354, 24.088, 22.527, 22.528, > > 22.126, 25.317, 25.643), env4 = c(0.047969, 0.003469, 0.003385, > > 0.002253, 0.000791, 0.001834, 0.008016, 0.009262, 0.003934, 0.002322, > > 0.00061, 0.000799, 6.4e-05, 0, 0, 0.000107, 0.003151, 0.018915, > > 0.015077, 0.004554, 0.003499, 0.002705, 0.003507, 0.001173, 0.000149, > > 0.000308, 0, 0, 0, 0.00074, 0.002845, 0.017047, 0.018915, 0.017111, > > 0.002417, 0.002668)), .Names = c("long", "lat", "sp1", "sp2", > > "sp3", "sp4", "env1", "env2", "env3", "env4"), class = "data.frame", row.names = c(NA, > > -36L)) > > > > predi<-dd[,7:10] > > > > spA<-subset(dd,dd$sp1==1) > > m1 <- maxent(spA[,7:10], spA[,'sp1']) # for species 1 > > m1.prediction<-predict(m1, predi) > > > > spB<-subset(dd,dd$sp2==1) > > m2 <- maxent(spB[,7:10], spB[,'sp2']) # for species 2 > > m2.prediction<-predict(m2, predi) > > > > spC<-subset(dd,dd$sp3==1) > > m3 <- maxent(spC,7:10], spC[,'sp3']) # for species 3 > > m3.prediction<-predict(m3, predi) > > > > > > spD<-subset(dd,dd$sp4==1) > > m4 <- maxent(spD[,7:10], spD[,'sp4']) # for species 4 > > m4.prediction<-predict(m4, predi) > > > > prediction<-cbind(m1.prediction,m2.prediction,m3.prediction,m4.prediction) > > > > But, it did not work. I could not figure it out why it is not working. Another thing, How I can make a loop for several species? > > cheers, > > KG > > > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]