next up previous
Next: Homework 2 Up: Homework and Project Assignments Previous: Homework 1

Script for Homework 1

Here are some of the code I used for Homework 1.

### problem 1
X = read.table("http://www.utdallas.edu/~ammann/stat3355scripts/SmokeCancer1.csv",
               header=TRUE,sep=",",row.names=1)
## obtain Cigarette sales rate per 100,000
CigSalesRate = 100000*X$FY2010Sales/X$Pop2010
## put variables into a data frame
CigCancer = data.frame(CigSalesRate = CigSalesRate,
                       CigPrice=X$CigPrice,
                       CigYouthRate=X$CigYouthRate,
                       CigAdultRate=X$CigAdultRate,
                       LungCancerRate=X$LungCancerRate,
                       Region=X$Region,
                       row.names=row.names(X))
### problem 2
rall = cor(CigCancer[,1:5]) # correlation matrix for all numeric variables
region.col = c("green","SkyBlue","magenta","gold") # define colors for boxplots
txndx = match("TX",row.names(X)) # find index of TX in row names
state.col = rep("black",dim(CigCancer)[1])
state.col[txndx]="red"
##
par(mfrow=c(2,2)) # 4 plots per page
## plot 1
plot(LungCancerRate ~ CigSalesRate, data=CigCancer,pch=19,col=state.col)
text(CigCancer$CigSalesRate[txndx],CigCancer$LungCancerRate[txndx],
     labels="Texas",pos=4,col="red")
title("Lung Cancer Rates vs FY2010 Cigarette Sales\nper 100,000 Population")
r2 = rall["LungCancerRate","CigSalesRate"]^2
text(max(CigCancer$CigSalesRate),min(CigCancer$LungCancerRate),
     labels=paste("R-squared = ",round(100*r2,1),"%",sep=""),pos=2)
### Problem 3
par(mfrow=c(2,2))
plot(CigSalesRate ~ Region, data=CigCancer,col=region.col)
title("FY2010 Cigarette Sales per 100,000 Population")
###
cat("\nProblem 7.\n\n")
allVals = as.matrix(CigCancer[,-6])
TXvals = allVals["TX",]
TX0 = scale(allVals,center=TXvals,scale=FALSE) <= 0
TXp = 100*apply(TX0,2,mean)
cat("Percentile rankings of Texas:\n")
TX.perc = paste(round(TXp,1),"%",sep="")
names(TX.perc) = names(TXvals)
print(TX.perc, quote=FALSE)
cat("\nThese represent the percentage of states with values less than or equal to\ncorresponding values for Texas\n")



ammann
2018-02-14