mahagadorn / r-world-mahagadorn

r-world-mahagadorn created by GitHub Classroom
0 stars 0 forks source link

Plants Simulation_Generating plant Array #2

Closed mahagadorn closed 8 years ago

mahagadorn commented 8 years ago

Hi Will,

I am having some issues with a few things and I will try to explain where my confusion is coming in...

In our plant.timestep() we are calling the arguments plants and info. I have no issues with the info argument, since we already generated this in a previous step.

My issue come in when calling the plants argument. I understand that this is simply an array that we generate in section 6.2.3, however, when making the array we call our plant.timestep and we add one to it. How is it that we can call a function plant.timestep that we can't make until we have generated the plant array, but we can't generate the plant array until we can used the plant.timestep.

I have pushed a script called "Test.Plants.R" where I am trying to test out my functions using a fake terrain matrix. When trying to generate the plants array I keep getting an error message. I have attached both the code and the comments (containing the error message) below. I commented somewhat within the message while trying to trouble shoot the error.


#how we make the plants array                                
plants <- array("", dim=c(dim(Test.Terrain), plant.timestep + 1))
for(i in seq_len(dim(plants)[3]))
  #seq_len(y) or in our case (seq_len(dim(plants)) is creating a sequence up dimensions of plants array
  plants[,,i][is.na(terrain)] <- NA

####Working through Error message
# Error in plant.timestep + 1 : non-numeric argument to binary operator
###MAH comment: this is saying that there is an issue with my binaary operator "+" because the plant.timestep isnt numeric
# > for(i in seq_len(dim(plants)[3]))
#   +   #seq_len(y) or in our case (seq_len(dim(plants)) is creating a sequence up dimensions of plants array
#   +   plants[,,i][is.na(terrain)] <- NA
# Error in seq_len(dim(plants)[3]) : 
#   argument must be coercible to non-negative integer
# In addition: Warning message:
#   In seq_len(dim(plants)[3]) : first element used of 'length.out' argument

I think my confusion is in multiple places, but once I figure out one I think it will likely clear up the others.

Mal

mahagadorn commented 8 years ago

Will,

This is kind of to my ducky, but mostly to you...

Thank you for working on this with me today. The way you explained it make senses.

I just wanted to clarify something, and I will try to put this is a reproducible way. Within our run.plant.ecosystem we should be generating our plants array and then looping over that array using plant.timestep. This should fill in the the arrays based on the time steps.

The code below is what I have for my test terrain matrix, timestep function , and for my run.plant.ecosystem.


#code for test.terrain
Test.Terrain <- matrix(NA, nrow=5, ncol=5)
Test.Terrain[1,] <- round(rnorm(5), digits = 3)
Test.Terrain[2,] <- round(rnorm(5, 3, 1), digits = 3)
Test.Terrain[3,] <- round(rnorm(5, 2, 1), digits = 3)
Test.Terrain[4,] <- round(rnorm(5, 1, .25), digits = 3)
Test.Terrain[5,] <- round(rnorm(5, 1, 1), digits = 3)

#Lets add in some NA's so that we can test the NA aspect of our functions
Test.Terrain[4,3] <- NA
Test.Terrain[3,3] <- NA

#time step function
plant.timestep <- function(plants, info){
  #define survivor function
  survive <- function(cell, info){
    if(is.na(cell))     #if it isnt a species I want you to return what is already in the contents of the cell
      return(NA)
    if (cell=='')
      return('')
    if(runif(1) <= info$survive[plant])   #your value is greater than or equal to your survival probability then you win yay!
      return(cell)
    if(runif(1) >= info$survive[plant])   #if the random number is greater that our survival probability the return a blank space
      return('')  #this makes sense because if it dies it's no longer there...there is nothing in this cell
  }
  #looping through the plant matrix
  for(i in plants){
    for(j in plants)
      for(k in plants)
      new.plant.matrix <- survive(plants[i,j,k], info)
    return(new.plant.matrix)
  }
}

#run.plant.ecosystem

run.plant.ecosystem <- function(terrain, num.timesteps, info){
  terrain <- terrain
  info <- info
  #Make the array
  plants <- array("", dim=c(dim(terrain), num.timesteps + 1))
  for(i in seq_len(dim(plants)[3]))
    #seq_len(y) or in our case (seq_len(dim(plants)) is creating a sequence up dimensions of plants array
    plants[,,i][is.na(terrain)] <- NA
  for(i in plants){
    print(i)          #only here it see whats printing out....ERASE LATER
    for(j in plants){
      print(j)        #only here it see whats printing out....ERASE LATER
      for(k in plants){
        print(k)     #only here it see whats printing out....ERASE LATER
        plants[i,j,k] <- plant.timestep(plants[,,k], info)
      }
    }
  }
}

run.plant.ecosystem(Test.Terrain, 2, info)

#error message:  Error in plants[, , k] : no 'dimnames' attribute for array

AHHH, I feel like I am sooooo close, but I am missing something! -Mal

willpearse commented 8 years ago

I think we discussed this in office hours, right?

mahagadorn commented 8 years ago

Yes we did... I will close it.