rosemckeon / ploidy

How does disturbance on a landscape affect the establishment of new polyploid plant species?
3 stars 0 forks source link

Improve check_germination #95

Closed rosemckeon closed 5 years ago

rosemckeon commented 5 years ago

Going through the simulation logs, I'm not sure this is working right. I think too many germinations are getting blocked. Even when there are only 100 adults and over 2000 seeds, sometimes all of them are getting blocked and It's happening too often to be by chance.

rosemckeon commented 5 years ago

function works absolutely fine. checking it's getting passed the right data.

rosemckeon commented 5 years ago

Looks like the right data too.

rosemckeon commented 5 years ago

@bradduthie After (mostly) fixing dispersal range I can see the seeds spreading about much better. So they should be giving rise to more adults. However, they seem to be getting blocked by the new germination rule. We don't get enough new juveniles and so adults don't appear. I can't see what's wrong with this function. If I test it on a simple set of data...

like:

adults <- tibble(
  X = 1,
  Y = 1
)
seeds <- tibble(
  X = rep(1:2, 5),
  Y = rep(1, 10)
)
check_germination(seeds, adults)

Seeds are split correctly into those on 1,1 (won't germinate) and those on 1,2 (will). I don't know why so many seeds are being blocked from germination.

(I'm also still fine-tuning the dispersal. It's working loads better but I've spotted a bunch of negative values in the X and Y columns so it's still going wrong somewhere)

rosemckeon commented 5 years ago

@bradduthie movement is fully sorted now. I'd fixed the clumping along edges, but they were still going out of bounds and looked weirdly clumped in cells even after that. Turns out movement was happening in bunches for some reason.

I'm a bit confused about how much I can trust this shiny landscape document though. In the data, I have adults that are within bounds of the landscape, but they disappear on the plots after a certain generation when they should be there :|

bradduthie commented 5 years ago

Greetings from Louisville! I'll take a look @rozeykex -- do you have some code that could recreate the problem, and what you printed off see what was going wrong? A second set of eyes might help pinpoint where the problem is (in the mean time, I'll pull and run a few times to see how things are working now).

rosemckeon commented 5 years ago

Hi @bradduthie! Thanks so much for getting back to me while you're away. A second pair of eyes would be really appreciated if you have time. I'm feeling a bit lost with it all. I hope your talk has gone/goes well.

There were a couple of edits I hadn't pushed (a parameter for seed dispersal). Those are up now. And I realised that the shiny landscape plots were just mislabelling the life stage levels so I've pushed an edit to that which leaves the legend showing numbers 1 through 3 for life stages. It now shows the adults I could see in the data.

The issue is definitely still there though. If you run the shiny presentation (/documents/presentation/testing-shiny.Rmd), load the most recent data in the presentation folder (./data/null-fixed-adults-3.rds) and look at the accompanying log in the same folder you should be able to see what I mean.

Specifically, look at generation 6 in the log https://github.com/rozeykex/ploidy/blob/31a2b8ef69338e13b3322aafd285f235f7844b6f/documents/presentation/data/null-fixed-adults-3-sim-1.txt#L981 Here no seeds germinate at all, apparently because of established adults. But the landscape plot in the presentation shows that there are plenty of seeds in locations without adults present.

So, firstly I had seeds clumping, limiting the adults that could appear from all the germinations. But now that's sorted I can see there is a problem with the germination rule too (which makes sense as that's when the population size plots went pear-shaped).

This is the code I used to run that simulation:

disturploidy(pop_size = 100, grid_size = 40, ploidy_growth_benefit = 0, 
    inbreeding_cost = 0, germination_prob = 0.4, max_growth_rate = 4, 
    adult_size = 1.3, N_ovules = 25, pollen_range = 40, fertilisation_prob = 0.75, 
    uneven_matching_prob = 0.75, selfing_polyploid_prob = 0.75, 
    selfing_diploid_prob = 0.75, triploid_mum_prob = 0.75, adult_survival_prob = 0.9, 
    juvenile_selection_constant = 0.1, seed_survival_prob = 0, 
    disturbance_freq = 0, ploidy_prob = 0, generations = generations, 
    simulations = simulations, filepath = "documents/presentation/data/", 
    filename = "null-fixed-adults-3", logfilepath = "documents/presentation/data/", 
    logfilename = "null-fixed-adults-3")
rosemckeon commented 5 years ago

@bradduthie think I might have sorted this. Pushing changes now.

rosemckeon commented 5 years ago

A quick test with simulations/quick-test.R over 5 generations in a 10X10 landscape showed adult population at near K = 100 and germinations blocked in the log looked reasonable when compared to landscape plots.

Running simulations/null.R now over 50 gens in a 40X40 landscape to see if that carries through before closing issue and going back to 200 gen sims.

bradduthie commented 5 years ago

Hi @rozeykex -- excellent job! I've pulled and run, and everything looks good from my end too. It's moving along slowly on my laptop, but I'm not getting any errors. The package compiles nicely.

I think that the idea of setting the mortality rate at 0.5 sounds good. The probability of an adult making it 5 generations is then $(1 - 0.5)^5 \approx 0.03$, which seems reasonable. It's enough so that some adults can be expected to persist for a couple generations, but not so many as to interfere with your ability to interpret changes in alleles over time.

Would it still be useful for me to go over this?

rosemckeon commented 5 years ago

@bradduthie thanks :) I think I'd like to know why the original check_germination function didn't work. I'm still not really sure why it didn't return the right results - I didn't really fix it, I just wrote a new way of doing it from scratch (get_those_not_outcompeted). If you can see what's up with it I'd appreciate any pointers!

Simulations do run slower again. Oh well. I think they must have only got faster because not enough germination was happening. Do you think we could go smaller with the grid size?

bradduthie commented 5 years ago

Hi @rozeykex -- I've looked through the check_germination function. I think that what is happening is that the if statements are failing to identify when the X-Y location of a seed overlaps with the X-Y location of an individual adult. Instead, when I tried the same idea with some dummy data, I was getting matches when a seed's X and Y locations matched with adult1's X and adult2's Y location. To fix this, I think you would need to subset the data. I started trying to do this, but then realised after making a subset of the data sub_seeds in which all X's match that of an adults, any subsequent which statements would not find the original rows in seeds. You could store the original values in seeds somehow, or use a double for loop (obviously not ideal).

The other option would be some sort of pairing function, essentially turning each seed and adult X and Y locations into a single, unique, number. Then, check to see if a seed's number is found in the adult.

cantor_pair <- function(X, Y){
    pi_XY <- 0.5 * (X + Y) * (X + Y + 1) + Y;
    return(pi_XY);
}

Then, if you have seed and adult X and Y elements:

seeds    <- NULL;
seeds$X  <- sample(x = 1:10, size = 1000, replace = TRUE);
seeds$Y  <- sample(x = 1:10, size = 1000, replace = TRUE);
adults   <- NULL;
adults$X <- sample(x = 1:10, size = 100, replace = TRUE);
adults$Y <- sample(x = 1:10, size = 100, replace = TRUE);
seed_ca  <- cantor_pair(seeds$X, seeds$Y);
adult_ca <- cantor_pair(adults$X, adults$Y);
seed_die <- which(seed_ca %in% adult_ca);

I think that your way in get_those_not_outcompeted is probably just as fast though, if not faster.

rosemckeon commented 5 years ago

Thanks @bradduthie - really appreciated feedback.