Skip to content
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

lesson 2 #3

Open
sylviakinosian opened this issue Nov 2, 2016 · 15 comments
Open

lesson 2 #3

sylviakinosian opened this issue Nov 2, 2016 · 15 comments

Comments

@sylviakinosian
Copy link
Collaborator

sylviakinosian commented Nov 2, 2016

Hi Will,

I have a couple questions about Lesson 2:

First is about #4. I've gotten it to work at this point, but it's not as neat as I think it could be. Instead of printing "Good" and "Job" for 5 (the only prime number also divisible by 5) I think the loop just sees the 5 and prints Good, but then it's all happy and doesn't get to the "Job" part of the loop. I fixed this by just creating an extra if statement:

if (i == 5){ cat("Good Job:", i, "\n") }

...but I feel like there must be a better way to accomplish this.

Second question is about #6, plotting a function through time. What I think I need the function to do is loop through the equation for each value of time. I've looked at some other people's code and they seem to be doing this with requesting start/end/by in the function to have the beginning, end, and index for time. Would it be possible to just have something like:

for (i in 1:length(time)){
a * exp(-b * exp(-c * [i]))
}

Also, I'm not sure how to go about looping through a function that has multiple variables, but just recalculating it with one changing variable (in this case time). I've totally guessed at the syntax above.

Thanks,
Sylvia

@willpearse
Copy link
Member

For question 4, you're wrong. It does print both things when i equals 5 when you remove that line. I think you've gotten a bit confused... :D

Your syntax for question 6 is actually fine, with the exception of the []. You only need those when you're subsetting a vector (pulling a part of it out). Otherwise, everything is fine! :D

@sylviakinosian
Copy link
Collaborator Author

Thanks, #4 does work just fine, haha!

As for #6, I've got:

gompTime <- function(time, a, b, c){
    pop <- c(NA)
    for (i in 1:length(time)){
      pop <- gompPop(i,a,b,c)
    }
    plot(x=time, y=pop, xlab="Time", ylab="Pop Size", main="Gomp Style")
}

but it only plots one point, the starting time. I cannot figure out how to get it to loop through all of the times.

When I add in pop[i] <- gompPop(i,a,b,c) I get the error: "Error in pop[i] <- gompPop(i, a, b, c) : replacement has length zero"

@willpearse
Copy link
Member

The clue is in the error: "replacement has length zero". This means your function gompPop is returning something of length zero. What does that mean? Well, it sounds awfully like it's not returning anything at all...

Take a look at your function gompPop and see if you can see what you're missing. Hint: read the above, and ask yourself if you want to be using cat...

@sylviakinosian
Copy link
Collaborator Author

Thanks! I got it!

Please disregard my most recent email/cry for help.

I do have a question about the solution I found for #7. I was getting so fed up with my for loops not working that I took it out and just stuck some variables in instead:

gompMagic <- function(time, a, b, c){
    t <- c(1:time)
    color <- "black"
    color[gompPop(t,a,b,c) > a] <- "blue"
    color[gompPop(t,a,b,c) > b] <- "red"
    plot(t, gompPop(t,a,b,c), xlab="Time", ylab="Pop Size", main="Gomp Style", type = "l", col = color)
}

I first tried color(gompPop(t,a,b,c) > a) and it didn't work.. but using brackets instead of parentheses did work. What's going on there? I thought that the brackets were for indexing, like with rows and cuddles columns and such...?

@willpearse
Copy link
Member

We've covered all this in the session now, I think. Thanks for getting in touch!

@sylviakinosian
Copy link
Collaborator Author

Thanks again for all your help this morning (well, afternoon for you I suppose).

In lesson 4, I'm trying to fix my summary function to use apply .. but I'm having issues.

reptar <- replicate(10, rnorm(1,rnorm(1),runif(1, min = 0)), simplify = "array")
apply(reptar,2,mean)
Error in apply(reptar, 2, mean) : dim(X) must have a positive length

I don't understand why it's staying that dim(X) must have a positive length because the length of my variable reptar is 10

@willpearse
Copy link
Member

You're going to kick yourself about this one - you've not changed the replicate step. You're making 10 draws of 1 variable, when you should be making 10 draws of 10 variables. When you make that change, it will work.

@sylviakinosian
Copy link
Collaborator Author

Ohhhhhhhhh.

I was confused about the usage of rep. I had initially tried:

rep(rnorm(1,rnorm(1),runif(1,min=0)),10)
 [1] 0.8993083 0.8993083 0.8993083 0.8993083 0.8993083 0.8993083 0.8993083 0.8993083
 [9] 0.8993083 0.8993083

But it actually should be:

rep(rnorm(10,rnorm(1),runif(1, min = 0)),1)
[1] -0.09183079 -1.04347949  0.81847672 -0.11950358 -0.00286057 -0.43425212
[7]  0.05637280  0.35094367 -1.05579044 -0.15682419

I guess I figured that replicate was actually the right thing to use here, because it's the function we used for number one in this lesson:

  1. replicate is an important function that allows you to quickly generate random numbers. Use it to create a dataset of 10 variables, each drawn from a Normal distribution with different means and variances. This can be achieved in one line.

and it seemed to be working:

replicate(10, rnorm(1,rnorm(1),runif(1, min = 0)), simplify = "array")
[1] -0.15349070  1.72369686 -0.87854895  0.85171595  0.40381606  1.06282286
[7] -1.30464218 -0.06803278  0.47566892  0.08757258

Anyway now that I think about it, my issues with dim(X) must have a positive length was stemming from the fact that I wasn't working with a data frame. So:

reptar <- rep(rnorm(10,rnorm(1),runif(1, min = 0)),1)
r2 <- rep(rnorm(10,rnorm(1),runif(1, min = 0)),1)
new <- cbind(reptar,r2)

Thanks again. :)

@willpearse
Copy link
Member

No!

@willpearse
Copy link
Member

Sorry, but I thought you might be online, so I wanted to make a quick comment - no!

Go back and read what I wrote again: you're still making the same mistake. Stick with replicate and draw 10 10 times, not 10 1 time. Look at the numbers, and look at what you've done...

@sylviakinosian
Copy link
Collaborator Author

uhh,

like this?

replicate(1, rnorm(10,rnorm(1),runif(1, min = 0)), simplify = "array")
            [,1]
 [1,] -0.3328263
 [2,] -0.3961033
 [3,] -0.6457380
 [4,]  0.6794058
 [5,] -0.2207727
 [6,] -0.1562414
 [7,]  0.2962150
 [8,]  0.3302082
 [9,] -0.0747946
[10,] -0.1220895

@sylviakinosian
Copy link
Collaborator Author

is grep a good thing to use for the categorical data summary?

@willpearse
Copy link
Member

No! You need 10 of those columns. So put the number 10 somewhere else in that function (perhaps replacing the number 1...)

@sylviakinosian
Copy link
Collaborator Author

replicate(10, rnorm(10,rnorm(1),runif(1, min = 0)), simplify = "array")

@willpearse
Copy link
Member

The crowd goes wild! Yes, that's it :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants