Skip to content

Instantly share code, notes, and snippets.

@marutter
Created October 14, 2010 18:11
Show Gist options
  • Save marutter/626693 to your computer and use it in GitHub Desktop.
Save marutter/626693 to your computer and use it in GitHub Desktop.
library(faraway)
data(savings)
attach(savings)
summary(savings)
help(savings)
pairs(savings)
#
# A better 'pairs' plot
#res <- lm(sr~pop15)
summary(res)
library(car)
scatterplot.matrix(savings)
scatterplot.matrix(savings,diagonal="boxplot")
scatterplot.matrix(savings,diagonal="histogram")
scatterplot.matrix(savings,diagonal="qqplot")
install.packages("psych")
library(psych)
pairs.panels(savings)
#
# You Can Write your Own
#
panel.cor <- function(x, y, digits=2, prefix="", cex.cor)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- cor(x, y)
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
test <- cor.test(x,y)
# borrowed from printCoefmat
Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("***", "**", "*", ".", " "))
text(0.5, 0.5, txt, cex = cex * abs(r))
text(.8, .8, Signif, cex=cex, col=2)
}
pairs(savings,
lower.panel=panel.smooth, upper.panel=panel.cor)
cor(savings)
cor.test(sr,pop15)
res <- lm(sr~pop15)
summary(res)
res2 <- lm(sr~pop15+pop75)
summary(res2)
res3 <- lm(sr~pop15+ddpi)
summary(res3)
res4 <- lm(sr~pop15+ddpi+pop75+dpi)
summary(res4)
res4b <- lm(sr~pop75+dpi+pop15+ddpi)
summary(res4b)
library(lattice)
cloud(sr~pop15+ddpi,pch=20)
install.packages("TeachingDemos")
library(TeachingDemos)
rotate.cloud(sr~pop15+ddpi,pch=20)
plot(res3,1)
qq.plot(res3$res)
plot(pop15,res3$res)
plot(ddpi,pop15)
plot(pop75,res3$res)
plot(dpi,res3$res)
summary(res3)
confint(res3)
nd <- data.frame(pop15=c(30,35),ddpi=c(700,100))
predict(res3,nd,interval="confidence")
nd <- data.frame(pop15=c(30,35,40),ddpi=c(700))
predict(res3,nd,interval="confidence")
anova(res3,res4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment