# Section 2; Question 1: # source('chap_11_sect_2_question_1_data.R') x = DF$Chirps_Per_Second y = DF$Temperature plot( x, y, type='p', pch=19, cex=1.5, xlab='Chirps per Second', ylab='Temperature' ) m = lm( y ~ x ) abline(m, col='red') grid() print(m) predict( m, newdata=data.frame(x=18) ) # Section 2; Question 2: # source('chap_11_sect_2_question_2_data.R' ) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_2_2_plot.eps", onefile=FALSE, horizontal=FALSE) plot( DF$Age, DF$Proof, type='p', pch=19, cex=1.5, xlab='Age', ylab='Proof' ) m = lm( Proof ~ Age, data=DF ) abline(m, col='red') grid() #dev.off() # Section 2; Question 3: # source( 'chap_11_sect_2_question_3_data.R' ) plot( DF$Temperature, DF$Parts_Dissolved, type='p', pch=19, cex=1.5, xlab='Temperature' , ylab='Parts Dissolved' ) m = lm( Parts_Dissolved ~ Temperature, data=DF ) abline(m, col='red') grid() r = residuals(m) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_2_3_residual_plot.eps", onefile=FALSE, horizontal=FALSE) plot( DF$Temperature, r, type='p', pch=19, cex=1.5, main='residual plot' ) grid() #dev.off() # Section 2; Question 7: # source('chap_11_sect_2_question_7_data.R') #postscript("../../WriteUp/Graphics/Chapter11/chap_11_2_7_plot.eps", onefile=FALSE, horizontal=FALSE) plot( DF$Spending_Per_Pupil, DF$Graduation_Rate, type='p', pch=19, cex=1.5, xlab='Spending Per Pupil', ylab='Graduation Rate' ) m = lm( Graduation_Rate ~ Spending_Per_Pupil, data=DF ) abline(m, col='red') grid() #dev.off() print( m ) # Section 2; Question 8: # source('../Chapter8/chap_8_sect_2_question_11_data.R') plot( DF$Plant_Diversity, DF$Bird_Diversity, type='p', pch=19, cex=1.5, xlab='Plant Diversity', ylab='Bird Diversity' ) m = lm( Bird_Diversity ~ Plant_Diversity, data=DF ) abline(m, col='red') grid() print( m ) r = residuals(m) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_2_8_residual_plot.eps", onefile=FALSE, horizontal=FALSE) plot( DF$Plant_Diversity, r, type='p', pch=19, cex=1.5, xlab='Plant Diversity', ylab='Residual' ) grid() #dev.off() # Section 2; Question 9: # source('chap_11_sect_2_question_9_data.R') plot( DF$Index_of_Exposure, DF$Cancer_Mortality, type='p', pch=19, cex=1.5, xlab='Index of Exposure', ylab='Mortality' ) m = lm( Cancer_Mortality ~ Index_of_Exposure, data=DF ) abline(m, col='red') grid() print( m ) r = residuals(m) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_2_9_residual_plot.eps", onefile=FALSE, horizontal=FALSE) plot( DF$Index_of_Exposure, r, type='p', pch=19, cex=1.5, xlab='Index of Exposure', ylab='Residual' ) grid() #dev.off() # Section 2; Question 10: # source('chap_11_sect_2_question_10_data.R') plot( DF$x, DF$y, type='p', pch=19, cex=1.5, xlab='x', ylab='y' ) m = lm( y ~ x, data=DF ) abline(m, col='red') grid() print( m ) r = residuals(m) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_2_10_residual_plot.eps", onefile=FALSE, horizontal=FALSE) plot( DF$x, r, type='p', pch=19, cex=1.5, xlab='x', ylab='Residual' ) abline(h=0, col='black', cex=1.5) grid() #dev.off() # Section 2; Question 11: # source('chap_11_sect_2_question_11_data.R') plot( DF$Plumage_Index, DF$Behavioral_Index, type='p', pch=19, cex=1.5, xlab='Plumage Index', ylab='Behavioral Index' ) m = lm( Behavioral_Index ~ Plumage_Index, data=DF ) abline(m, col='red') grid() print( m ) r = residuals(m) plot( DF$Plumage_Index, r, type='p', pch=19, cex=1.5, xlab='Plumage_Index', ylab='Residual' ) abline(h=0, col='black', cex=1.5) grid() # Section 2; Question 15: # source('chap_11_sect_2_question_15_data.R') d = DF$Distance v = DF$Velocity b = sum( d*v ) / sum( d*d ) print( b ) # Section 2; Question 17: # source('chap_11_sect_2_question_17_data.R') a_star = 100 numer = sum( DF$Years * DF$Percent ) - a_star * sum( DF$Years ) denom = sum( DF$Years * DF$Years ) b = numer / denom print( b ) # Section 2; Question 18: # source('../Chapter8/chap_8_sect_2_question_5_data.R') plot( DF$Number_of_Suites, DF$Projected_Revenue, pch=19, cex=1.5, xlab='Number of Suites', ylab='Projected Revenue' ) m = lm( Projected_Revenue ~ Number_of_Suites - 1, data=DF ) abline(m, col='red') grid() print( m ) # Section 2; Question 20: # source('chap_11_sect_2_question_20_data.R') x = DF$Days_After_Injection y = log(DF$Serum_Gold_Concentration) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = a exp( b x ) # a = exp( coefficients(m)[1] ) b = coefficients(m)[2] print( sprintf( 'Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b) ) x = DF$Days_After_Injection y = DF$Serum_Gold_Concentration plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Days After Injection', ylab='Serum Gold Concentration' ) x_grid = seq( min(x), max(x), length.out=50 ) y_hat = a * exp( b * x_grid ) lines( x_grid, y_hat, type='l', col='red' ) grid() # Compute the halflife: # print( sprintf( 'halflife= %10.6f', log(1/2) / b) ) # Section 2; Question 21: # source('chap_11_sect_2_question_21_data.R') x = DF$Years_After_1995 y = log(DF$Gross_Federal_Debt) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = a exp( b x ) # a = exp( coefficients(m)[1] ) b = coefficients(m)[2] print( sprintf('Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b) ) x = DF$Years_After_1995 y = DF$Gross_Federal_Debt plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Years After 1995', ylab='Gross Federal Debt' ) x_grid = seq( min(x), max(x), length.out=50 ) y_hat = a * exp( b * x_grid ) lines( x_grid, y_hat, type='l', col='red' ) grid() # x0 = 2007 - 1995 y_hat = a * exp( b * x0 ) print( sprintf('x0= %3.0f; h_hat= %10.6f', x0, y_hat) ) # Plot the residuals of the nonlinear model: # #postscript("../../WriteUp/Graphics/Chapter11/chap_11_sect_2_ex_21.eps", onefile=FALSE, horizontal=FALSE) y_hat = a * exp( b * x ) plot( x, y-y_hat, pch=19, cex=1.5, main='residual plot', xlab='Years After 1995' ) grid() #dev.off() # Section 2; Question 22: # source('chap_11_sect_2_question_22_data.R') x = DF$Age y = log(DF$Suggested_Retail_Price) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = a exp( b x ) # a = exp( coefficients(m)[1] ) b = coefficients(m)[2] print( sprintf('Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b) ) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_sect_2_ex_22.eps", onefile=FALSE, horizontal=FALSE) x = DF$Age y = DF$Suggested_Retail_Price plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Age', ylab='Suggested Retail Price' ) x_grid = seq( min(x), max(x),length.out=50 ) y_hat = a * exp( b * x_grid ) lines( x_grid, y_hat, type='l', col='red' ) grid() #dev.off() # x0 = 11 y_hat = a * exp( b * x0 ) print( sprintf('x0= %3.0f; h_hat= %10.6f', x0, y_hat) ) # Section 2; Question 23: # source('chap_11_sect_2_question_23_data.R') x = DF$Years_After_1981 y = log(DF$Dow_Jones_Industrial_Average) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = a exp( b x ) # a = exp( coefficients(m)[1] ) b = coefficients(m)[2] print( sprintf('Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b)) x = DF$Years_After_1981 y = DF$Dow_Jones_Industrial_Average plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Years After 1981', ylab='Dow Jones Industrial Average' ) x_grid = seq( min(x), max(x), length.out=50 ) y_hat = a * exp( b * x_grid ) lines( x_grid, y_hat, type='l', col='red' ) grid() # Section 2; Question 25: # source('chap_11_sect_2_question_25_data.R') x = log(DF$Peak_Wind_Gust) y = log(DF$Number_of_Damaged_Homes) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = a x^b => log(y) = log(a) + b log(x) # a = exp( coefficients(m)[1] ) b = coefficients(m)[2] print( sprintf('Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b) ) x = DF$Peak_Wind_Gust y = DF$Number_of_Damaged_Homes plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Peak Wind Gust', ylab='Number of Damaged Homes') x_grid = seq( min(x), max(x), length.out=50 ) y_hat = a * x_grid^b lines( x_grid, y_hat, type='l', col='red' ) grid() # Section 2; Question 26: # source('chap_11_sect_2_question_26_data.R') x = log(DF$Foraging_Size) y = log(DF$Colony_Size) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = a x^b => log(y) = log(a) + b log(x) # a = exp( coefficients(m)[1] ) b = coefficients(m)[2] print( sprintf('Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b) ) x0 = 2500 y0 = a * x0^b print( sprintf('x0= %10.2f, y0= %10.2f', x0, y0) ) # Section 2; Question 27: # source('chap_11_sect_2_question_27_data.R') x = log(DF$Striate_Cortex) y = log(DF$Prestriate_Cortex) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = a x^b => log(y) = log(a) + b log(x) # a = exp( coefficients(m)[1] ) b = coefficients(m)[2] print( sprintf('Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b) ) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_sect_2_ex_27.eps", onefile=FALSE, horizontal=FALSE) x = DF$Striate_Cortex y = DF$Prestriate_Cortex plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Striate Cortex', ylab='Prestriate Cortex') x_grid = seq( min(x), max(x), length.out=50 ) y_hat = a * x_grid^b lines( x_grid, y_hat, type='l', col='red' ) grid() #dev.off() # Section 2; Question 28: # source('chap_11_sect_2_question_28_data.R') x = 1/DF$Distance_From_Center_of_City y = DF$Value plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) y_hat = predict( m, newdata=data.frame(x=4) ) print( sprintf('x= %.3f; y_hat= %.3f', 1/4, y_hat) ) # Section 2; Question 30: # source('chap_11_sect_2_question_30_data.R') L = 1055 x = DF$Years_After_1959 y = log(L/DF$Number_of_ICBMs - 1) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = L / ( 1 + e^{a+b x} ) # a = coefficients(m)[1] b = coefficients(m)[2] print( sprintf('Nonlinear model parameters: a= %10.6f, b= %10.6f', a, b) ) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_sect_2_ex_30.eps", onefile=FALSE, horizontal=FALSE) x = DF$Years_After_1959 y = DF$Number_of_ICBMs plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Years After 1959', ylab='Number of ICBMS') x_grid = seq( min(x), max(x), length.out=50 ) y_hat = L / ( 1 + exp( a + b * x_grid ) ) lines( x_grid, y_hat, type='l', col='red' ) grid() #dev.off() # Section 2; Question 31: # source('chap_11_sect_2_question_31_data.R') L = 60 x = DF$Age_Mid_Point y = log(L/DF$Percent_Improved - 1) plot( x, y, pch=19, cex=1.5, main='linearized data' ) m = lm( y ~ x ) abline(m, col='red') grid() print( m ) # Get the parameters of the model y = L / ( 1 + e^{a+b X} ) # a = coefficients(m)[1] b = coefficients(m)[2] print( sprintf('Nonlinear model parameters; a= %10.6f, b= %10.6f', a, b) ) #postscript("../../WriteUp/Graphics/Chapter11/chap_11_sect_2_ex_31.eps", onefile=FALSE, horizontal=FALSE) x = DF$Age_Mid_Point y = DF$Percent_Improved plot( x, y, pch=19, cex=1.5, main='data and model', xlab='Age', ylab='Percent Improved') x_grid = seq( min(x), max(x), length.out=50 ) y_hat = L / ( 1 + exp( a + b * x_grid ) ) lines( x_grid, y_hat, type='l', col='red' ) grid() #dev.off()