Holcombe:ProgrammingInR

From OpenWetWare

(Difference between revisions)
Jump to: navigation, search
m (Setting up a proxy in R on a Mac: fixed up the manual proxy address given)
Current revision (20:00, 13 September 2012) (view source)
(doing ANOVAs etc)
 
(9 intermediate revisions not shown.)
Line 18: Line 18:
-
===dataframe stuff===
+
===dataframe tips===
Examining your data frame or object, let's say it's called ''datos''
Examining your data frame or object, let's say it's called ''datos''
  typeof(datos) #returns "list!"
  typeof(datos) #returns "list!"
Line 44: Line 44:
Check your counterbalancing in your results file. Make a contingency table,
Check your counterbalancing in your results file. Make a contingency table,
  table(dataRaw$speed,dataRaw$relPhaseOuterRing)
  table(dataRaw$speed,dataRaw$relPhaseOuterRing)
 +
 +
Replace certain value with another
 +
thr$thresh[  thr$task=='ident' ] = NA
==Creating Graphs (usu. ggplot2)==
==Creating Graphs (usu. ggplot2)==
-
Good advice on [http://biostat.mc.vanderbilt.edu/twiki/pub/Main/StatGraphCourse/graphscourse.pdf graph choice, from human perceptual standpoint] by Frank Harrell
 
-
In the lab we usually use the package, [http://had.co.nz/ggplot2/ ggplot2], for graphs. Ask Sarah about the ggplot2 book.
+
how I [[Holcombe:fit psychometric functions]] and bootstrap
-
For custom colour schemes, the [http://research.stowers-institute.org/efg/R/Color/Chart/ Chart of R Colors] is a helpful resource. The table with named colours is most useful.
+
See http://openwetware.org/wiki/Holcombe:Plotting
-
 
+
-
ggplot2 tips:
+
-
#where 'g' is your ggplot object
+
-
str(g)  #gives you everything!
+
-
summary(g) #gives a summary
+
-
last_plot() #refreshes the last plot and returns the struct
+
-
 
+
-
p=p+scale_y_continuous(breaks=c(0,0.5,1),minor_breaks=c(.25,.75)) #changing axis breaks
+
-
p=p+opts(title="one-behind errors")
+
-
p=p+xlab('relative phase')
+
-
 
+
-
opts(panel.grid.minor = theme_blank())+
+
-
 
+
-
last_plot() + opts(panel.grid.minor = theme_line(colour = "black") )
+
-
 
+
-
facet_grid(.~subject)  # rows~columns
+
-
 
+
-
g<-g+stat_summary(fun.y=mean,geom="point",position="jitter") #getting jitter to work when you're collapsing across other variables with stat_summary
+
-
 
+
-
g=g+ stat_smooth(method="glm",family="binomial",fullrange=TRUE) #adds logistic smoother to plot
+
-
#it's impossible to extract the function fits however because they're fit on the fly
+
-
#how can I fit an arbitrary function, like a psychometric function, e.g. cumulative gaussian with chance rate and lapse rate? Logistic is restricted to 0->1
+
-
+
-
 
+
-
order used for scale mapping (color, etc.) is perhaps the order of the levels property of the vector. This gives bizarre results because R's sort(unique(x)) default does not alphabetize strings.
+
-
 
+
-
how I [[Holcombe:fit psychometric functions]] and bootstrap
+
==Debugging in R==
==Debugging in R==
Line 88: Line 63:
==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 94: 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 101: Line 84:
==Setting up a proxy in R on a Mac==
==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:
+
The easiest way to set up a proxy is simply to create a file called ".Rprofile" (make sure the file does not have a .txt or .rtf extension. Textwrangler will automatically give it an extension, it may be best to create the file using Vi in a terminal - using emacs probably won't work) in your user directory (~ or /Users/username/) with the line:
<code>
<code>
   Sys.setenv(http_proxy=”http://username:password@tcdproxy.tcd.ie:8080″)
   Sys.setenv(http_proxy=”http://username:password@tcdproxy.tcd.ie:8080″)
Line 108: Line 91:
This information (and more) can be found on [http://www.kenbenoit.net/?p=261 Ken Benoit's webpage]
This information (and more) can be found on [http://www.kenbenoit.net/?p=261 Ken Benoit's webpage]
-
For Sydney Uni, use:
+
If you're at the Camperdown campus of the University of Sydney, use:
 +
<code>
 +
  Sys.setenv(http_proxy=”http://www-cache.usyd.edu.au:8080″)
 +
</code>
 +
 
 +
Note: The following may not necessarily be true anymore. If the below address does not work, use the same one as for Camperdown above.
 +
The Cumberland Campus of the University of Sydney, use:
<code>
<code>
-
   Sys.setenv(http_proxy=”www-cache.usyd.edu.au:8080″)
+
   Sys.setenv(http_proxy=”web-cache-ext.usyd.edu.au:8080″)
</code>
</code>

Current revision

Members

Alex Holcombe
Sarah McIntyre
Fahed Jbarah
• Shih-Yu Lo
• Patrick Goodbourn
Lizzy Nguyen
Alumni


Technical

Skills Checklist
Python Programming
Psychopy/VisionEgg Installation Notes
R analysis,plot,stats
Statistics
Buttonbox
Verifying timing
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.

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" (make sure the file does not have a .txt or .rtf extension. Textwrangler will automatically give it an extension, it may be best to create the file using Vi in a terminal - using emacs probably won't work) in your user directory (~ or /Users/username/) with the line:

 Sys.setenv(http_proxy=”http://username:password@tcdproxy.tcd.ie:8080″)

Then restart R. This information (and more) can be found on Ken Benoit's webpage

If you're at the Camperdown campus of the University of Sydney, use:

 Sys.setenv(http_proxy=”http://www-cache.usyd.edu.au:8080″)

Note: The following may not necessarily be true anymore. If the below address does not work, use the same one as for Camperdown above. The Cumberland Campus of the University of Sydney, use:

 Sys.setenv(http_proxy=”web-cache-ext.usyd.edu.au:8080″)
Views
Personal tools