# Day 2 R scripts ############################################ ############################################# # Salary Data # ############################################# # You should first read the data in... either using the following command # or use the "Import Dataset" buttom #SalaryData = read.table("SalaryData.txt",header=T) attach(SalaryData) plot(Gender,Salary,col="red",lwd=1.5,main="Salary Data") plot(YrHired,Salary,xlab="Year Hired") points(YrHired[Gender=="Male"],Salary[Gender=="Male"],col="blue",pch=19) points(YrHired[Gender=="Female"],Salary[Gender=="Female"],col="red",pch=19) plot(YrsPrior,Salary) points(YrHired[Gender=="Male"],Salary[Gender=="Male"],col="red",pch=19) points(YrHired[Gender=="Female"],Salary[Gender=="Female"],col="blue",pch=19) model1 = lm(Salary~Gender) model2 = lm(Salary~Gender+YrHired) plot(YrHired,Salary,xlab="Year Hired") abline(coef=model2$coef[c(1,3)],col="red",lwd=2) points(YrHired[Gender=="Female"],Salary[Gender=="Female"],col="red",pch=19) abline(coef=c(model2$coef[1]+model2$coef[2],model2$coef[3]),col="blue",lwd=2) points(YrHired[Gender=="Male"],Salary[Gender=="Male"],col="blue",pch=19) model3 = lm(Salary~Gender*YrHired) plot(YrHired,Salary,xlab="Year Hired") abline(coef=model3$coef[c(1,3)],col="red",lwd=2) points(YrHired[Gender=="Female"],Salary[Gender=="Female"],col="red",pch=19) abline(coef=c(model3$coef[1]+model3$coef[2],model3$coef[3]+model3$coef[4]),col="blue",lwd=2) points(YrHired[Gender=="Male"],Salary[Gender=="Male"],col="blue",pch=19) ## Another Housing Data ####################### #MidCity = read.table("MidCity.txt",header=T) attach(MidCity) n = dim(MidCity)[1] dn1 = rep(0,n) dn1[Nbhd==1]=1 dn2 = rep(0,n) dn2[Nbhd==2]=1 dn3 = rep(0,n) dn3[Nbhd==3]=1 BR = rep(0,n) BR[Brick=="Yes"]=1 Price = Price/1000 SqFt = SqFt/1000 MidCityModel = lm(Price~dn1+dn2+SqFt) plot(SqFt,Price,xlab="Size") abline(coef=MidCityModel$coef[c(1,4)],col=2,lwd=2) points(SqFt[dn3==1],Price[dn3==1],col=2,pch=19) abline(coef=c(MidCityModel$coef[1]+MidCityModel$coef[2],MidCityModel$coef[4]),col=4,lwd=2) points(SqFt[dn1==1],Price[dn1==1],col=4,pch=19) abline(coef=c(MidCityModel$coef[1]+MidCityModel$coef[3],MidCityModel$coef[4]),col=3,lwd=2) points(SqFt[dn2==1],Price[dn2==1],col=3,pch=19) legend(1.45,210,c("Nbhd = 1","Nbhd = 2","Nbhd = 3","Just Size"),lty=c(1,1,1,2),lwd=c(2,2,2,2),col=c(4,3,2,1)) abline(lsfit(SqFt,Price),col=1,lwd=2,lty=2) Nbhd = factor(Nbhd) model2 = lm(Price~Nbhd + SqFt + Nbhd:Brick)