# Holcombe:ProgrammingInR

(Difference between revisions)
 Revision as of 02:13, 6 April 2010 (view source) (→Creating Graphs (usu. ggplot2))← Previous diff Revision as of 02:14, 6 April 2010 (view source) (→Creating Graphs (usu. ggplot2))Next diff → Line 53: Line 53: last_plot() last_plot() - #changing axis breaks + p=p+scale_y_continuous(breaks=c(0,0.5,1),minor_breaks=c(.25,.75)) #changing axis breaks - + - p=p+scale_y_continuous(breaks=c(0,0.5,1),minor_breaks=c(.25,.75)) + p=p+opts(title="one-behind errors") p=p+opts(title="one-behind errors") - p=p+xlab('relative phase') p=p+xlab('relative phase') - - #panel.grid.minor opts(panel.grid.minor = theme_blank())+ opts(panel.grid.minor = theme_blank())+ Line 68: Line 63: facet_grid(.~subject)  # rows~columns facet_grid(.~subject)  # rows~columns - g<-g+stat_summary(fun.y=mean,geom="point",position="jitter") + 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 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. 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.

## Revision as of 02:14, 6 April 2010

### Members

Alex Holcombe
Polly Barr
• Charlie Ludowici
• Kim Ransley
• Ingrid Van Tongeren
William Ngiam
Fahed Jbarah
• Patrick Goodbourn
Alumni

### Other

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. Calling typeof() on a dataframe returns "list"! Delete nearly everything in memory: rm(list = ls())

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

```head(datos)
str(datos)
summary(datos) #good for ggplot objects also
typeof(datos)
```

### dataframe stuff

df\$varWithExtraLevels = factor(df\$varWithExtraLevels)

library(Hmisc);

describe(df)

Calling typeof() on a dataframe returns "list"

rm(objectToBeDeleted)

length(df) #number of columns of dataframe

names(df) #names of columns of dataframe

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.

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

```table(dataRaw\$speed,dataRaw\$relPhaseOuterRing)
```

## Creating Graphs (usu. ggplot2)

In the lab we usually use the package, ggplot2, for graphs. Ask Sarah about the ggplot2 book.

For custom colour schemes, the Chart of R Colors is a helpful resource. The table with named colours is most useful.

ggplot2 tips:

```last_plot()
```
```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
```

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.

## 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
```

## fitting psychometric functions

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 inside R going to 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 Package Installer inside R, "Local Package Directory" option, after in my case first deleting original PsychoFun installation in /Library/Frameworks/R.framework/Versions/2.10/Resources/library/

Technical Report explains many more terms than JoV article: Acceptance rate:Next sample in chain only accepted if quantity on p.481 of JoV paper is good Kinetic, Potential energy from Hamiltonian algorithm

## doing ANOVAs etc

I think I had too many error terms reducing error terms

## 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" 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

For Sydney Uni, use: ``` ```

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