CrumpLab / psyc7709_2019

Course website for PSYC 7709 - Using R for Reproducible Research (taught by Matt Crump 2019)
https://crumplab.github.io/psyc7709_2019/
1 stars 7 forks source link

Wrangling accuracy #13

Open mhorger opened 5 years ago

mhorger commented 5 years ago

I'm struggling to successfully calculate the accuracy of the flanker trials. This is the code I'm currently working with:

accuracy %>%  
cbind(accuracy, accurate = 0) %>%
for (i in accuracy){
  if ((accuracy$response) == (accuracy$center_letter)){
      accurate <- 1} 
  }

I've tried other versions but I frequently get an error which says it can only calculate for the first value which I assume means my loop isn't working correctly.

I've also tried using indexing (ex. accuracy[i,14]), but that produces even more error messages.

I did get the loop to work for the subsequent problem about RT so I think I'm having a misunderstanding about combining a loop and an if/else statement.

CrumpLab commented 5 years ago

Interesting approach, I'm not sure that you can add a loop after a %>% operator.

Consider the following. I assume that you have been able to make a new column in the dataframe that contains the center letter in lower case. Below, I make a small dataframe, and then show how dplyr could be used to get an accuracy column using mutate to add a new column.

response <- c("d","d","h","h","d")
center_letter <- c("d","d","d","h","d")
df <- data.frame(response,center_letter)

df <- df %>%
           mutate(accuracy = response==center_letter)

Note that response == center_letter produces a vector of TRUE and FALSE values. The mutate function computes that vector, then adds a column with the results with the name accuracy.

mhorger commented 5 years ago

That worked and was a much easier solution than what I was attempting.

Why is it that in cases like this you don't have to specify a loop for it to fill in all the values?

CrumpLab commented 5 years ago

The short answer is that R supports vectorized operations that allow some things to be done without loops. In this case, we could have used a loop, but we don't have to. Throughout the rest of the semester we will learn more about vectorize operations. Generally, using the dplyr syntax we won't be using loops.

Also, FYI, I'm starting to add more examples of vectorized approaches here on the course website:

https://crumplab.github.io/psyc7709/ExampleCode.html#vectorized_approaches

And, I will be adding even more basic examples here to try to help distinguish between loop style and R vectorized style.

mhorger commented 5 years ago

Thanks so much! I've been working on creating the table to display overall reaction time and when I google it, the internet suggests downloading additional packages including knitr or xtable. Is one of these preferred for use in our class? Or should I be able to do this without downloading an additional package?

CrumpLab commented 5 years ago

I added a section on tables to the course website https://crumplab.github.io/psyc7709/Advanced.html#tables

Tables can be complicated in R, and there are numerous packages for getting what you want.

For a short answer I often using the kable function from the knitr package to print dataframes as tables. And, I often use xtable with kable to print the results of ANOVAs. There are examples of both on the course website.

You probably already have knitr installed. You can call the kable function without loading knitr as a library, like in the following that usesknitr::kable() to print a data frame.

df <- data.frame(A=1,
                 B=2,
                 C=3,
                 D=4)
knitr::kable(df)