pearselab / r-intro-jenessalemon

r-intro-jenessalemon created by GitHub Classroom
0 stars 1 forks source link

lesson_2 #13 #9

Open jenessalemon opened 7 years ago

jenessalemon commented 7 years ago

Will and @Wolflab: I think that writing the algorithm out in English first actually helped a lot. Here is my English part:

Create function that takes the number of sites, and vectors of species, and of their p and lambda values initialize an empty vector the same size as the number of species evaluate each species with species.abundance store the results in a matrix return the matrix

I messed around with the syntax and what I came up with starts on line 295. The error says: Error in if (species.presence(p) == 1) { : missing value where TRUE/FALSE needed In addition: Warning message: In rbinom(1, 1, p) : NAs produced

The thing that is confusing me is that the errors are coming from the two previous functions that I am calling (species.presence and species.abundance) rather than my new function (hurdle). The previous functions work fine on their own?

Wolflab commented 7 years ago

Everything looks pretty good tome. The thing that looks a bit strange is you are looping across the matrix. The indexing might actually be correct, but I am not sure. You know that each returning vector from the abundance function is a vector across sites. So now you must be looping across species. Can you see a way to do that, other than looping across the "results" matrix? Not sure I am correct but I think this is worth testing

jenessalemon commented 7 years ago

For looping through the values is the only way I know how to fill in a matrix. However, it's possible that I need another for loop to do the computation and then a separate for loop to store the values in the matrix. Let me try that. Thanks for the advice!

Wolflab commented 7 years ago

No - you do indeed need to loop through the matrix. But you what you are doing is looping through species, then you need to use the top index to change the NA into the correct numbers. Almost there. I am sure you can do this by looping through the matrix, but I think you might be doing this blindly without knowing if you are looping through rows or columns. But I need to check on that

Wolflab commented 7 years ago

Above I meant to say loop index. I think you are close

jenessalemon commented 7 years ago

I just did another commit, I'm not sure if I've gotten closer or father away! Here is the error message: Error in values[i] <- species.abundance(1, pvals[i], lambdas[i]) : replacement has length zero

jenessalemon commented 7 years ago

I've tried looping through both species (columns) and nsites (rows), and I still get the original error: Error in if (species.presence(p) == 1) { : missing value where TRUE/FALSE needed In addition: Warning message: In rbinom(1, 1, p) : NAs produced

Any ideas? Sorry to be such a bother.

Wolflab commented 7 years ago

I see an error in the species.abundance function. You have returned sites on line 280 WITHIN the else loop, rather than after then next set of curly braces. This means that essentially your function breaks as soon as you get a zero abundance.

Make sense?

paul

jenessalemon commented 7 years ago

It makes sense that I needed to return "sites" after the curly braces, but what doesn't make sense is why the function still worked? It doesn't seem to me like the loop was breaking when it hit a 0, because I ran it a bunch of times and got 5 values each time (which is how many I was aiming for.) And, after I fixed that I am still getting the same error, it didn't fix the "hurdle" function. Doing my best not to get frustrated. 👍

Wolflab commented 7 years ago

You were so very very close.. But the problem was simply matrix indexing. I have been making the same mistakes. I am not sure what matrix[i] is. But I do know that my.matrix [i,] is the ith row. But you want to replace columns (you are looping over species, and each species is in its own column). See if you can go back to your earlier version and and make one tiny tiny correction and it will work like a charm