# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # #----- save_plots = F # # EPage 140 # DF = read.csv( "../../Data/tumor_size.csv", header=TRUE ) DF_long = reshape( DF, varying=c( "Days_11", "Days_13", "Days_15", "Days_17" ), v.names=c( "size" ), timevar="days", idvar="Mouse", direction="long" ) rownames(DF_long) = NULL DF_long[DF_long$days==1,2] = 11 DF_long[DF_long$days==2,2] = 13 DF_long[DF_long$days==3,2] = 15 DF_long[DF_long$days==4,2] = 17 if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter6/tgim_spaghetti_plot.eps", onefile=FALSE, horizontal=FALSE) } interaction.plot(DF_long$days, DF_long$Mouse, DF_long$size, xlab="days", ylab="size", main="spaghetti plot of tumor size by mouse", legend=F) if( save_plots ){ dev.off() } # Fit a linear model: # m = lm( size ~ days, data=DF_long ) summary(m) if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter6/tgim_linear_fit.eps", onefile=FALSE, horizontal=FALSE) } par(mfrow=c(1,2)) plot( DF_long$days, DF_long$size ) abline(m) plot(m, which=1) par(mfrow=c(1,1)) if( save_plots ){ dev.off() } # Try a lograthmic transformation of size: # DF_long$lsize = log(DF_long$size) m2 = lm( lsize ~ days, data=DF_long ) summary(m2) if( save_plots ){ postscript("../../WriteUp/Graphics/Chapter6/tgim_linear_N_log_fit.eps", onefile=FALSE, horizontal=FALSE) } plot( DF_long$days, DF_long$size ) abline(m, col='red') days = unique(DF_long$days) y_hat = exp( predict( m2, newdata=data.frame(days=days) ) ) points( days, y_hat, type='l', col='green' ) if( save_plots ){ dev.off() } # Fit a linear model on each mouse separately: # all_coeffs = data.frame() for( mi in 1:10 ){ mm = lm( size ~ days, data=DF_long[ DF_long$Mouse==mi, ] ) mc = coef(mm) F = data.frame( mouse=mi, intercept=mc[1], slope=mc[2] ) rownames(F) = NULL all_coeffs = rbind( all_coeffs, F ) } all_coeffs = all_coeffs[ order(all_coeffs$slope), ] print(all_coeffs)