Skip to content

Instantly share code, notes, and snippets.

@marutter
Created November 4, 2010 18:23
Show Gist options
  • Select an option

  • Save marutter/662901 to your computer and use it in GitHub Desktop.

Select an option

Save marutter/662901 to your computer and use it in GitHub Desktop.
#
# Modeling Interactions
#
library(faraway)
data(savings)
attach(savings)
summary(lm(sr~pop15+ddpi))
summary(lm(sr~pop15:ddpi))
summary(lm(sr~pop15+ddpi+pop15:ddpi))
summary(lm(sr~pop15*ddpi))
summary(lm(sr~(pop15+ddpi)^2))
summary(lm(sr~(pop15+ddpi+pop75)^2))
summary(lm(sr~(pop15*ddpi*pop75)^2))
#
# Categorical Variables
#
data <- read.csv("salary.csv")
attach(data)
plot(YSdeg,Salary,main="Salary vs. Years")
plot(YSdeg,Salary,pch=as.numeric(Sex),main="By Gender")
plot(YSdeg,Salary,pch=as.numeric(Rank),main="By Rank")
library(ggplot2)
qplot(YSdeg,Salary,shape=Sex)
qplot(YSdeg,Salary,shape=Rank,color=Rank)
#
# Analysis
#
# Different intercept, same slope
res1 <- lm(Salary~YSdeg+Sex)
summary(res1)
# Different slope, same intercept
res2 <- lm(Salary~YSdeg+Sex:YSdeg)
summary(res2)
# Different intercept and slop
res3 <- lm(Salary~YSdeg+Sex+Sex:YSdeg)
summary(res3)
res0 <- lm(Salary~YSdeg)
res <- lm(Salary~YSdeg+Rank)
summary(res)
Rank <- relevel(Rank,"Asst Prof")
resrl <- lm(Salary~YSdeg+Rank)
summary(resrl)
plot(YSdeg,Salary,pch=as.numeric(Rank),main="By Rank")
abline(17166.46,95.07,col="red")
abline(17166.46+4209,95.07,col="green")
abline(17166.46+10310,95.07,col="blue")
qplot(YSdeg,Salary,shape=Rank,color=Rank) + geom_abline(intercept=17166.46,slope=95.07,color="red") +
geom_abline(intercept=17166.46+4209,slope=95.07,color="green")+
geom_abline(intercept=17166.46+10310,slope=95.07,color="blue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment