-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error in reproduce_model
examples documentation
#65
Comments
I need to upload it again because I included a '.' where I shouldn't have in the DESCRIPTION file, so you can push the change. |
OK, I will push it up. Meanwhile, I am having trouble creating a simple example of a user-defined null model that calls ##################
# Create your own null model
# Simple test for fit of data to a Poisson
# vector set up as a data frame for null_model_engine
MyData <- data.frame(c(0,0,0,1,2,50))
names(MyData) <- "N"
# Calculate the variance to mean ratio of the data
# For a true Poisson, this should ~ 1.0
MyMetric <- function(x=runif(10)){
VarMeanRatio <- var(x)/mean(x)
return(VarMeanRatio)
}
# Take a data vector
# Calculate its mean
# Treat that as lambda
# Simulate a data set of the same size
MyAlgo <- function(x=runif(10)){
lambda <- mean(x)
sim <- rpois(length(x),lambda)
return(sim)
}
# functions work on vectors, but this code throws an error:
MyModel <- null_model_engine(speciesData="MyData",algo="MyAlgo", metric="MyMetric", type=NULL) Is this a problem because I am using a vector rather than a matrix? When this is straightened out, please add this code to the Examples section of the documentation for |
@ngotelli There are two problems actually. 1). When you make your own function the first arguments need to follow the ecosimr conventions of having the first argument be MyData <- c(0,0,0,1,2,50)
colnames(MyData) <- "N"
#Calculate the variance to mean ratio of the data
# For a true Poisson, this should ~ 1.0
MyMetric <- function(m=runif(10)){
VarMeanRatio <- var(m)/mean(m)
return(VarMeanRatio)
}
# Take a data vector
# Calculate its mean
# Treat that as lambda
# Simulate a data set of the same size
MyAlgo <- function(speciesData=runif(10)){
lambda <- mean(speciesData)
sim <- rpois(length(speciesData),lambda)
return(sim)
}
# functions work on
MyModel <- null_model_engine(speciesData= MyData ,algo="MyAlgo", metric="MyMetric",nReps = 1000)
summary(MyModel)
plot(MyModel)
Inputting your data as a dataframe will work too...
|
Thanks; I had tried it before with no quotes on the data frame and still had troubles. I am sure with hard work and study I will eventually master this program! 🐑 |
Do you think there's a way we could make it easier and more intuitive? |
Hi @emhart The
I will try to work more on this soon. N. |
Hi @emhart . Sorry to bother you again, but I am still having trouble getting # Example #2
# Construct a source pool and a parameter for species weights
# Draw randomly from the source pool and count the number of species present
# This is just a poor man's rarefaction program
# Create the sourcepool of 26 alphabet species
MySourcePool <- paste("Species",LETTERS,sep="")
# Create an island assemblage with 64 individuals and 6 species:
MyData <- paste("Species",c(rep("A",50),rep("B",10),"C","D","E","F"),sep="")
# Create a vector of relative species colonization weights
MyWeights <- sort(rbeta(n=length(MySourcePool),shape1=0.5,shape2=0.5),decreasing=TRUE)
# "algo" function for null model algorithm
# Draw a random sample from the source pool, sampling with replacement and species weights
MyAlgo <- function(speciesData=runif(10),weights=runif(100),sourcepool=runif(100)){
NullAssemblage <- sample(x=sourcepool,size=length(speciesData),replace=TRUE,prob=weights)
return(NullAssemblage)
}
# "metric" function for null model metric
# give the species count for the random sample
MyMetric <- function(m=LETTERS){
SpeciesCount <- length(unique(m))
return(SpeciesCount)
}
MyModel <- null_model_engine(speciesData=MyData,algo="MyAlgo",metric="MyMetric",algoOpts=list(weights=MyWeights,sourcepool=MySourcePool))
summary(MyModel)
plot(MyModel) I have confirmed the behavior of the two functions ( Thanks for your help! Nick |
@ngotelli I tracked this down to some error handling. If a data frame's first column is text, the software strips out the first column and then reclasses the data as numeric. This is to handle the case where a user inputs a matrix with species names in the first column and the rest of the numeric values happen to be text. In your case, all your data is text. It get's replaced with NA's so you get 0. This behaviour is to try and make data input seamless for the user. However if we just allow text inputs, we lose error handling in the case of a text matrix being entered. So I'm not sure of a way around this. |
@emhart Ah, that's insidious, and it is the same error that tripped me up when I was testing co-occurrence a while back. To patch this, I will try adding row names to all of my vectors so that when they are stripped out I should still be left with my character vectors. One possible solution (which does not have to implemented now) would be to create a new function A related issue is that it seems counter-intuitive that new functions for For now, I see you are getting close to having Enjoy your weekend, N. |
@ngotelli actually, it will probably still not work because the data you're passing in is text. No matter what it will try and convert it to a number because it's expecting numeric inputs, so you'll probably get a new error. I didn't realize we would accept text inputs. Here I try and adopt what you're doing but with counts, not sure if I get at the spirit of what your example was trying... MyData <- table(paste("Species",c(rep("A",50),rep("B",10),"C","D","E","F"),sep=""))
MyWeights <- sort(rbeta(n=length(MyData),shape1=0.5,shape2=0.5),decreasing=TRUE)
MyAlgo <- function(speciesData,weights) {
NullAssemblage <- rmultinom(1,size=sum(speciesData),prob=weights)
return(NullAssemblage)
}
MyMetric <- function(m){
return(sum(m > 0))
}
MyModel <- null_model_engine(speciesData=MyData,algo="MyAlgo",metric="MyMetric",algoOpts=list(weights=MyWeights))
summary(MyModel)
plot(MyModel) Regarding the parameter names, the reason I do this is because the null model engine uses |
Hi @emhart Yes, your code does the same thing with numeric variables. I think it is fine to restrict EcoSimR format to numeric to avoid these kind of problems; I just wasn't thinking about it when I started coding. Eventually, I'd like to get both of these (corrected) examples included with the documentation for We can revisit the general structure of the user-defined null models some time later. Best, Nick |
Hi @emhart
There is a small error in the documentation for the
reproduce_model
examples code. We have:However, in two of the lines, the correct function call should be:
reproduce_model(finchMod)
Since you have already submitted to CRAN, I wasn't sure if it was safe to push any changes to the repo now.
The text was updated successfully, but these errors were encountered: