cssearcy / AYS-R-Coding-SPR-2020

Coding in R for Policy Analytics
https://cssearcy.github.io/AYS-R-Coding-SPR-2020/
3 stars 3 forks source link

Lab 3 - Question 6 - Adding Average Line #14

Open BriannaSmithR opened 4 years ago

BriannaSmithR commented 4 years ago

For some reason, my code isn't producing an average line at all? I'm trying to do it the exact same as the video on youtube, but my graph isn't changing. I was hoping someone could take a look at my code and tell me if something is obviously wrong because I've been looking at it for a long time, and I feel like I'm going crazy.

lines(x = league.year,
      y = league.avg,
      col = "deepskyblue4",
      lwd = 2)

points(x = league.year,
       y = league.avg,
       col = "deepskyblue4",
       cex = 2,
       pch = 16)
lecy commented 4 years ago

That code is fine, so it must be something in a data step. How are you create league.year and league.avg?

jamisoncrawford commented 4 years ago

@BriannaSmithR what Dr. @lecy says is spot on. Something is occurring upstream that we can't make head nor tail of without that code. Would you like to email me your .Rmd file and I'll take a look?

jamisoncrawford commented 4 years ago

@BriannaSmithR it's indeed an issue with the preprocessing - specifically these lines of code:

league.year <- league.year[index]
league.avg <- league.year[index]

...the second line should be...

league.year <- league.year[index]
league.avg <- ave.so[index]

...found in this preprocessing chunk (fixed here):

ave.so <- Teams$SO / Teams$G
year <- Teams$yearID

ave.so.min <- min(ave.so, na.rm = TRUE)
ave.so.max <- max(ave.so, na.rm = TRUE)           # Plot window dimensions

league.ave <- tapply(X = ave.so, 
                     INDEX = as.factor(year), 
                     FUN = "mean", 
                     na.rm = TRUE)                # League average/season

league.year <- as.numeric(names(league.ave))

index <- which(year >= 1900 & year <= 2012)

year <- year[index]
ave.so <- ave.so[index]

index <- which(league.year >= 1900 & league.year <= 2012)

league.year <- league.year[index]
league.avg <- ave.so[index]

The approach to finding this issues was just printing the contents of object league.avg. While I expected to find values between 1 and 10, it was a series of years - so I revisited the portion of the script where the object was created by Ctrl + F searching ave.so <-. Good on you for subsetting the data to only 2012! A lot of learners miss this step.

Hope this helps!

jamisoncrawford commented 4 years ago

P.S. Alternatively you could also use year and ave.so but it's understandable to rename them!

meeraomar commented 4 years ago

@jamisoncrawford I have a general query - are we supposed to use the reformatted rmd in our icollege or the one in lab instructions? Because I've been following the icollege one, and copy pasting the complete code to the next code chunk. I've understood up till question 8, I'm lost on what question 9 and 10 are asking for.

Also I've pretty much just followed the scripts you've used in your videos, with edits and additions. Is that okay?

jamisoncrawford commented 4 years ago

@meeraomar following the videos is perfectly fine and I encourage use of the supplemental videos, including using the same code shown verbatim. That's a general permissions thing.

You can copy and paste each code chunk, expanding on it each time, or you can do it in a fell swoop/single code chunk, which is likely easier. It's up to you!

If you made it to question 8, that's more than passable, but happy to help with more specific issues!