Category Archives: Uncategorized
experimenting with the updated ggplot2 package
ggplot2 (v 0.9.2) was recently released. To see what’s new use the following: news(Version == “0.9.2″, package = “ggplot2″) I was playing around with it when I was making some regression discontinuity plots in R. I wrote a function for … Continue reading
experimenting with maps in R
A number of resources exist for making elegant maps in R, especially from Hadley Wickham (http://had.co.nz/ggplot2/). The maps package and ggplot2 work well together to make national, state, or county borders. I need school district borders for a specific exercise. … Continue reading
playing with ggplot 2
I have been playing around with ggplot2 a lot recently. One thing I like about ggplot2 is the ability to condense a lot of information into graphical summaries. Sometimes, I need a quick and easy way to look at survey … Continue reading
Formatting Character Strings in R
Formatting characters from lower to upper case: x<-c(“a”,”b”,”c”,”d”) toupper(x) From upper to lower case: x<-c(“A”,”B”,”C”) tolower(x) #all lower case Capitalize words in R: #install.packages(“Hmisc”) if you don’t have it already library(Hmisc) x<-c(“arizona”,”california”,”indiana”,”illinois”) capitalize(x) #[1] “Arizona” ”California” “Indiana” … Continue reading
Flat Tables
This is a nice little function that I learned about in John Verzani’s book. It makes table() output look a little nicer: data<-data.frame(x=rbinom(100,1,.3),y=rbinom(100,1,.5)) #Mock up a quick toy for the example ftable(table(data$x,data$y)) 0 1 0 40 29 1 13 18
Indexing Vectors and Data Frames in R
First create a vector containing some values, like this: x<-c(6,7,8,9,10,NA,NA) Now try the following: x[1] #gives the first value of x x[c(1,3,5)] … Continue reading
Exporting Data
Exporting data from R to some other format is just as simple as getting the data in. I like working with comma-separated value files (.csv) but sometimes I want to export directly into Stata or SPSS. Let’s create a (correlated) … Continue reading
Installing Packages
Packages contain collections of functions. The R community has generated over 1,000 different packages. R come with a base set of packages including the ‘stats’ package. You will probably at some point want to install additional packages that can handle … Continue reading
More on Introductory Functions
Here are a bunch of introductory functions that I use all the time. If I haven’t said it before, I’ll say it now, the ‘#’ symbol is the start of my comments. Anything to the right of the ‘#’ symbol … Continue reading
Quick Numerical and Graphical Summaries of Data
Let’s generate a small dataset to work with x<-rnorm(100) y<-rnorm(100)+10 #The mean of y will be 10 units higher than x #Let’s correlate these data using a Pearson correlation of .4 r<-.4 y<-x*r+y*sqrt(1-r^2) g<-c(rep(“m”,50),rep(“f”,50)) We can put each of these … Continue reading
More lme Examples
http://www.maths.anu.edu.au/~johnm/r-book/xtras/mlm-lme.pdf
lmer from Gelman
http://www.stat.columbia.edu/~cook/movabletype/archives/2006/01/fitting_multile.html
lme from University of Michigan
http://www.stat.columbia.edu/~cook/movabletype/archives/2006/01/fitting_multile.html
Interesting ggplot2 Trick
http://www.r-bloggers.com/a-quick-ggplot2-hack-multiple-dataframes/
Some Basic Functions
Here are a couple of functions that are from scratch. The basic form is always the same: x<-function(){ #open the function here #body of function return() #value of list vector to be returned } #close the function here #Try out … Continue reading
Quick R’s Take on Power Functions
http://www.statmethods.net/stats/power.html
Chi Square GOF Test
The following chunk of code will create a function to conduct a chi-square goodness of fit test in R. #Professor Example – Goodness of fit #Do the observed favorability proportions depart markedly from their #expected value? enroll<-c(32,25,10) expected<-c(22.3,22.3,22.3) rbind(enroll,expected) chi<-sum(((enroll-expected)^2)/expected) … Continue reading
Quick R’s Take on ANOVA (aov)
http://www.statmethods.net/stats/anova.html I also posted some other notes on BB for ANOVA.
Rattle
http://cran.r-project.org/web/packages/rattle/index.html
Haven’t found any great solutions to the missing data issue in SPSS files…
I came along this forum (http://r.789695.n4.nabble.com/R-for-Windows-GUI-closes-when-I-try-to-read-spss-td862748.html) and it comes to a solution we considered which was exporting data to a .csv and then reading it into R. But that isn’t helpful if you have a lot of variables to recode … Continue reading
dchisq and dnorm Plots
I tried this for plotting out some chi-square distributions with various df and it seemed to work out: s<-seq(0,25,.01) plot(s,dchisq(s,2),type=”l”,lwd=2,col=”dodgerblue4″) lines(s,dchisq(s,5),type=”l”,lwd=2,col=”green1″) lines(s,dchisq(s,10),type=”l”,lwd=2,col=”orange1″) This should look something like this: Here’s something similar for a normal distribution (notice that I use slightly … Continue reading
The R Journal
I just found this (http://journal.r-project.org/) and found some great packages in the (few) issues that the group has published. It’s definitely worth checking out.
Simple Simulation for Helping to Learn R
I have found it useful to create quick and simple vectors and data frames within R since I know that much of the real world data I might be working with may have certain peculiarities that sometimes impede the learning … Continue reading
edit() and fix()
So I’ve discovered that fix() command has been more helpful to me when I want to edit a data frame and potentially multiple vectors while edit() looks like it is best used with single vectors. So far I haven’t found … Continue reading
Getting SPSS and Stata Data Files Into R
I use SPSS and Stata quite a bit and want to know how to get .sav and .dta files into R. Getting generic data files into R (e.g. .csv & .txt) are fairly simple and handled with R’s base library. … Continue reading
Importing Data (.csv and .txt)
First task in getting data into R is finding where your default working directory is. Start R an type the following command: dir() This will show the files in your current working directory. If you installed R using default settings, … Continue reading