Sunday, August 4, 2013

R - commands cheat sheet

To read a text file
> data <- read.csv('R - anon answers to total answers.txt',sep=",",header=TRUE)

To convert cells (row-column pairs) satisfying a certain condition to NA.
> data$Column[data$Column == 0] <- NA
> data$Column[data$Column == -Inf] <- NA

To generate a linear model from the data
> linear=lm(data[,2]~data[,1])
> polynomial=lm(data[,2]~data[,1]+I(data[,1]^2))
> loglinear=lm(logdata[,2]~logdata[,1])

To get a summary of the generated models
> summary(linear)
> summary(polynomial)

To plot data
> plot(data[,1],data[,2])
> plot(data[,2]~data[,1])
> plot(log(data[,1]),log(data[,2])) 

Adding text to plot
> text(c(300,300),c(0.8,0.82),labels=c("R^2 = 0.0001","y = 0.00072 * x - 0.163"))

To draw the linear model generated using "lm" command
> abline(linear)

We can draw polynomial curve generated using lm/glm using
> lines(sort(x), linear$fitted.values[order(x)])

To generate log-linear models using the glm package
> glm_test2=glm(data[,2]~data[,1],family=poisson())
> glm_test2=update(glm_test,.~.+I(data[,1]^3),family=poisson())

We can compare different linear models generated using lm or glm commands through anova tests. When there are more than 2 models, model-1 is compared to null hypothesis, model-2 with model-1, model-3 with model-2 and so on.
> anova(glm_test,glm_test2,glm_test3,glm_test4,glm_test5,glm_test6,glm_test7,glm_test8,glm_test9,glm_test10,test = "Chisq")


No comments:

Post a Comment