I was searching for some basic syntax in R (basically cross tabs and density plots) and I came across the Quick R site.
Its really a nice site for R beginners and anyone trying to remember some syntax.
R syntax can be very simple- a histoigram is just hist(), boxplot is just boxplot() and t test is just t.test(dataset)
Here is an example from the site-
http://www.statmethods.net/graphs/density.html
# Simple Histogram
hist(mtcars$mpg)
# Colored Histogram with Different Number of Bins
hist(mtcars$mpg, breaks=12, col="red")
# Add a Normal Curve (Thanks to Peter Dalgaard)
x <- mtcars$mpg
h<-hist(x, breaks=10, col="red", xlab="Miles Per Gallon",
main="Histogram with Normal Curve")
xfit<-seq(min(x),max(x),length=40)
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit, yfit, col="blue", lwd=2)
Histograms can be a poor method for determining the shape of a distribution because it is so strongly affected by the number of bins used.
KERNEL DENSITY PLOTS
Kernal density plots are usually a much more effective way to view the distribution of a variable. Create the plot using plot(density(x)) where x is a numeric vector.
# Kernel Density Plot
d <- density(mtcars$mpg) # returns the density data
plot(d) # plots the results
# Filled Density Plot
d <- density(mtcars$mpg)
plot(d, main="Kernel Density of Miles Per Gallon")
polygon(d, col="red", border="blue")
COMPARING GROUPS VIA KERNAL DENSITY
The sm.density.compare( ) function in the sm package allows you to superimpose the kernal density plots of two or more groups. The format is sm.density.compare(x, factor) where x is a numeric vector and factor is the grouping variable.
# Compare MPG distributions for cars with
# 4,6, or 8 cylinders
library(sm)
attach(mtcars)
# create value labels
cyl.f <- factor(cyl, levels= c(4,6,8),
labels = c("4 cylinder", "6 cylinder", "8 cylinder"))
# plot densities
sm.density.compare(mpg, cyl, xlab="Miles Per Gallon")
title(main="MPG Distribution by Car Cylinders")
# add legend via mouse click
colfill<-c(2:(2+length(levels(cyl.f))))
legend(locator(1), levels(cyl.f), fill=colfill)
It is not as exhaustive as http://cran.r-project.org/doc/manuals/R-intro.html
but it is much more simpler and easy to follow.
The site is created by Robert I. Kabacoff, Ph.D.
and he is working on a book called “R in Action”
I have received numerous requests for a hardcopy version of this site, so over the past year I have been writing a book that takes the material here and significantly expands upon it. If you are interested, early access is available.
If you have not been to that website, I recommend it highly (though the tagline or logo of R for SAS/SPSS/Stata users seems a bit familiar)-http://www.statmethods.net/index.html
Quick-R
for SAS/SPSS/Stata Users
Related Articles
- Two Thoughts on Lisp Syntax. (kazimirmajorinc.blogspot.com)
- Some Basics about Stats (psipsychologytutor.org)
- Bone Density Tests: A Clue to Your Future (webmd.com)
- Net Access Corporation Unveils 50,000 Square Foot, State-of-the-Art Data Center in Parsippany, New Jersey (prweb.com)
- programming languages – What makes lisp macros so special – Stack Overflow (stackoverflow.com)
- Thinking about Syntax (latenightpc.com)
- Our minds use syntax to understand actions, just like with language [Mad Psychology] (io9.com)
- Syntax highlighting for Django using Pygments (ofbrooklyn.com)
- People of HTML5 – Bruce Lawson (hacks.mozilla.org)
- Haskell syntax vs. Lisp syntax | LispCast (lispcast.com)