Holcombe:ProgrammingInR: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
No edit summary
 
(37 intermediate revisions by 3 users not shown)
Line 5: Line 5:


In the lab we have the book ''Using R for Introductory Statistics''. [[R_Statistics]] introduces you to R
In the lab we have the book ''Using R for Introductory Statistics''. [[R_Statistics]] introduces you to R
Dani has posted some example code and graphs on his [http://www.dlinares.org/Site/R_code.html personal website].


[http://www.rpad.org/Rpad/R-refcard.pdf R reference cheatsheet], also
[http://www.rpad.org/Rpad/R-refcard.pdf R reference cheatsheet], also
Line 11: Line 13:


There is a wiki with some good tips [http://wiki.r-project.org/rwiki/doku.php?id=start here]. Also
There is a wiki with some good tips [http://wiki.r-project.org/rwiki/doku.php?id=start here]. Also
[http://wiki.r-project.org/rwiki/doku.php?id=tips:data-frames Data frame tips],  [http://alittleknowledge.wordpress.com/2009/09/11/r-for-pedestrians/ list of R websites]
[http://rwiki.sciviews.org/doku.php?id=tips:tips#data Data frame tips],  [http://alittleknowledge.wordpress.com/2009/09/11/r-for-pedestrians/ list of R websites]
 
Functions in R can only return one parameter.
 


Functions in R can only return one parameter. Calling typeof() on a dataframe returns "list"!
===dataframe tips===
Delete nearly everything in memory: rm(list = ls())
Examining your data frame or object, let's say it's called ''datos''
typeof(datos) #returns "list!"
Examining your data matrix or object, let's say it's called ''datos''
str(datos) #tells you it's a dataframe, number of observations, columns, etc
  head(datos)
  head(datos)
  str(datos)
  str(datos)
summary(datos) #good for ggplot objects also
df$varWithExtraLevels = factor(df$varWithExtraLevels)
length(df) #number of columns of dataframe
names(df) #names of columns of dataframe
library(Hmisc);
describe(df)
#Calling typeof() on a dataframe returns "list"
rm(objectToBeDeleted)
rm(list = ls())  #Delete nearly everything in memory
expand.grid() to create dataframe with every combination of some factors
Check your counterbalancing in your results file. Make a contingency table,
table(dataRaw$speed,dataRaw$relPhaseOuterRing)
Replace certain value with another
thr$thresh[  thr$task=='ident' ] = NA
==Creating Graphs (usu. ggplot2)==
how I [[Holcombe:fit psychometric functions]] and bootstrap
See http://openwetware.org/wiki/Holcombe:Plotting


Don't use the function ''attach''. It seems to leave lots of data in the 'environment' that can cause problems later. Also it makes the code harder to understand.
==Debugging in R==
==fitting psychometric functions==
How to examine and try things with  a questionable variable within a function?
Malte Kuss hosts the R library PsychoFun on his personal webpage rather than c-ran server. So you must download is package, unzip it, and install it by going to R->Packages&Data->Package Installer->Local Package Directory->Install, go inside the PsychoFun directory you've unzipped, and click Open.


I needed to constrain width of psychometric function to be quite narrow. Prior I was using followed lognormal distribution. Then if want mode to be say .1, have to feed it a mean parameter of -2.3 because ln(.1) = -2.  Unfortunately the PsychoFun code doesn't allow using a negative parameter for that prior, so I had to change the code. To do so, you go into the downloaded version of PsychoFun folder before you install it, where you can find PsychoFun.R in the R subdirectory. I commented out line 56. Then have to reinstall with Install manager inside R, after in my case first deleting original PsychoFun installation in /Library/Frameworks/R.framework/Versions/2.10/Resources/library/
  ee <<- resultsMeans #make global, violating all principles of good coding #DEBUG
  STOP
 
After an error, calling traceback() gives you the stack


==doing ANOVAs etc==
==doing ANOVAs etc==
[http://ww2.coastal.edu/kingw/statistics/R-tutorials/formulae.html Understanding model formulae]
[http://ww2.coastal.edu/kingw/statistics/R-tutorials/repeated.html ANOVA with repeated measures] walk-through
[http://www.personality-project.org/r/r.anova.html some aov (ANOVA) explanation]
[http://www.personality-project.org/r/r.anova.html some aov (ANOVA) explanation]


Line 33: Line 73:
I think I had [http://tolstoy.newcastle.edu.au/R/help/04/08/2136.html too many error terms]
I think I had [http://tolstoy.newcastle.edu.au/R/help/04/08/2136.html too many error terms]
[http://books.google.com.au/books?id=ptbcCBSWvvQC&pg=PA359&lpg=PA359&dq=ANOVA+within-subjects++pooled+error+term&source=bl&ots=73eFDsaw8l&sig=ReTGWRjNFGnYQ4DF-D6qAE9qJgQ&hl=en&ei=cKu4Sd7CPIr2sAPSw4Q8&sa=X&oi=book_result&resnum=8&ct=result reducing error terms]
[http://books.google.com.au/books?id=ptbcCBSWvvQC&pg=PA359&lpg=PA359&dq=ANOVA+within-subjects++pooled+error+term&source=bl&ots=73eFDsaw8l&sig=ReTGWRjNFGnYQ4DF-D6qAE9qJgQ&hl=en&ei=cKu4Sd7CPIr2sAPSw4Q8&sa=X&oi=book_result&resnum=8&ct=result reducing error terms]
[http://blog.gribblelab.org/2009/03/09/repeated-measures-anova-using-r/ Anovas with repeated measures] can be complicated in R.
We have some R books in the lab


==Dealing with circular data==
==Dealing with circular data==
Line 38: Line 82:


see Swindale, N. V. (1998). Orientation tuning curves: empirical description and estimation of parameters. Biol Cybern, 78(1), 45-56.
see Swindale, N. V. (1998). Orientation tuning curves: empirical description and estimation of parameters. Biol Cybern, 78(1), 45-56.
==Setting up a proxy in R on a Mac==
The easiest way to set up a proxy is simply to create a file called ".Rprofile" in your user directory (~ or Users/username/) with the line:
<code>
  Sys.setenv(http_proxy=”http://username:password@tcdproxy.tcd.ie:8080″)
</code>
Then restart R.
This information (and more) can be found on [http://www.kenbenoit.net/?p=261 Ken Benoit's webpage]
For Sydney Uni, use:
<code>
  Sys.setenv(http_proxy=”http://www-cache.usyd.edu.au:8080″)
</code>

Latest revision as of 18:29, 6 June 2017

Recent members

Alex Holcombe
• Ryo Nakayama



Technical

Skills Checklist
Python Programming
Psychopy/VisionEgg Installation Notes
R analysis,plot,stats
Statistics
Buttonbox
Buttonbox with photocell
Programming Cheat Sheets


R is an interactive programming language for statistics. The syntax is very idiosyncratic, and not really in a good way. Try R for programmers for a description. However it may have menu-driven versions maybe available R commander we haven't tried that and another one is pmg GTK maybe here

In the lab we have the book Using R for Introductory Statistics. R_Statistics introduces you to R

Dani has posted some example code and graphs on his personal website.

R reference cheatsheet, also a file here Media:Matlab-python-xref.pdf‎ that gives equivalent code for doing array operations in MATLAB, Python, and R plot parameters

There is a wiki with some good tips here. Also Data frame tips, list of R websites

Functions in R can only return one parameter.


dataframe tips

Examining your data frame or object, let's say it's called datos

typeof(datos) #returns "list!"
str(datos) #tells you it's a dataframe, number of observations, columns, etc
head(datos)
str(datos)
summary(datos) #good for ggplot objects also
df$varWithExtraLevels = factor(df$varWithExtraLevels)
length(df) #number of columns of dataframe
names(df) #names of columns of dataframe
library(Hmisc); 
describe(df)
#Calling typeof() on a dataframe returns "list"
rm(objectToBeDeleted)
rm(list = ls())  #Delete nearly everything in memory

expand.grid() to create dataframe with every combination of some factors

Check your counterbalancing in your results file. Make a contingency table,

table(dataRaw$speed,dataRaw$relPhaseOuterRing)

Replace certain value with another

thr$thresh[  thr$task=='ident' ] = NA

Creating Graphs (usu. ggplot2)

how I Holcombe:fit psychometric functions and bootstrap

See http://openwetware.org/wiki/Holcombe:Plotting

Debugging in R

How to examine and try things with a questionable variable within a function?

 ee <<- resultsMeans #make global, violating all principles of good coding #DEBUG
 STOP

After an error, calling traceback() gives you the stack

doing ANOVAs etc

Understanding model formulae

ANOVA with repeated measures walk-through

some aov (ANOVA) explanation

R will assume factor is regressor if numeric

I think I had too many error terms reducing error terms

Anovas with repeated measures can be complicated in R.

We have some R books in the lab

Dealing with circular data

von Mises vs. wrapped Gaussian,

see Swindale, N. V. (1998). Orientation tuning curves: empirical description and estimation of parameters. Biol Cybern, 78(1), 45-56.