{smcl} {com}{sf}{ul off}{txt}{.-} name: {res} {txt}log: {res}/Users/eric/Work/MB/ATCR/200/Labs/lab 9.smcl {txt}log type: {res}smcl {txt}opened on: {res}15 Oct 2018, 08:36:25 {txt} {com}. . /* Suppose you run a moving company and want to predict the number of hours > of labor for a prospective job. You have collected some data on labor hours, > number of rooms in the pickup location, and the volume of the items to be > moved. We will use these data to build a regression model for labor hours. > > We will read in the data two ways, first using the input command: */ . . clear {txt} {com}. input laborhrs rooms volume {txt} laborhrs rooms volume 1{com}. 24.00 3.5 545 {txt} 2{com}. 13.50 2.0 400 {txt} 3{com}. 26.25 2.5 562 {txt} 4{com}. 25.00 3.0 540 {txt} 5{com}. 9.00 1.0 220 {txt} 6{com}. 20.00 3.0 344 {txt} 7{com}. 22.00 3.5 569 {txt} 8{com}. 11.25 2.0 340 {txt} 9{com}. 50.00 5.0 900 {txt} 10{com}. 12.00 1.5 285 {txt} 11{com}. 38.75 5.0 865 {txt} 12{com}. 40.00 4.5 831 {txt} 13{com}. 19.50 3.0 344 {txt} 14{com}. 18.00 2.5 360 {txt} 15{com}. 28.00 4.0 750 {txt} 16{com}. 27.00 3.5 650 {txt} 17{com}. 21.00 3.0 415 {txt} 18{com}. 15.00 2.5 275 {txt} 19{com}. 25.00 3.0 557 {txt} 20{com}. 45.00 5.5 1028 {txt} 21{com}. 29.00 4.5 793 {txt} 22{com}. 21.00 3.0 523 {txt} 23{com}. 22.00 3.5 564 {txt} 24{com}. 16.50 2.5 312 {txt} 25{com}. 37.00 4.0 757 {txt} 26{com}. 32.00 3.5 600 {txt} 27{com}. 34.00 4.0 796 {txt} 28{com}. 25.00 3.5 577 {txt} 29{com}. 31.00 3.0 500 {txt} 30{com}. 24.00 4.0 695 {txt} 31{com}. 40.00 5.5 1054 {txt} 32{com}. 27.00 3.0 486 {txt} 33{com}. 18.00 3.0 442 {txt} 34{com}. 62.50 5.5 1249 {txt} 35{com}. 53.75 5.0 995 {txt} 36{com}. 79.50 5.5 1397 {txt} 37{com}. end {txt} {com}. . /* Each observation is on separate line, with items separated by a space. > > We could also read in the data from the Excel file on the class website, using > this command: */ . . import excel using "lab 9.xlsx", firstrow clear {res}{txt} {com}. . /* The firstrow option tells Stata that the firstrow of the Excel file gives the variable names. > > As usual, we will add labels to clarify the variable definitions, which is > more convenient for programming than trying to encode the information in long, > complicated, hard-to-type variable names. */ . . label var laborhrs "Labor hours for moving job" {txt} {com}. label var rooms "Number of rooms" {txt} {com}. label var volume "Volume of moving job items (cubic feet)" {txt} {com}. . . /* First, we will plot the outcome vs each of the two predictors, adding a > lowess line to check for a non-linear relationship. */ . . local ylabel : var label laborhrs {txt} {com}. foreach x in rooms volume {c -(} {txt} 2{com}. local xlabel : var label `x' {txt} 3{com}. twoway (scatter laborhrs `x') (lowess laborhrs `x'), /// > ytitle("`ylabel'") ylabel(, angle(horizontal)) /// > xtitle("`xlabel'") legend(off) /// > name(lowess_`x', replace) {txt} 4{com}. {c )-} {res}{txt} {com}. . /* These plots suggest that labor hours may be slightly non-linear in the number of rooms, which could hold if places with more rooms also tend to have bigger rooms and thus more items per room, or require more elaborate packing. The evidence for non-linearity in the effects of volume seems weaker, primarily driven by the last two observations on the right. > > Both relationships are sufficiently linear for the correlations to be meaningful, although they only capture the linear part of the relationship and thus may understate the overall relationship in the presence of non-linearity: */ . . pwcorr laborhrs rooms volume, sig obs {txt}{c |} laborhrs rooms volume {hline 13}{c +}{hline 27} laborhrs {c |} {res} 1.0000 {txt}{c |} {c |}{res} 36 {txt}{c |} rooms {c |} {res} 0.8638 1.0000 {txt}{c |}{res} 0.0000 {txt}{c |}{res} 36 36 {txt}{c |} volume {c |} {res} 0.9430 0.9333 1.0000 {txt}{c |}{res} 0.0000 0.0000 {txt}{c |}{res} 36 36 36 {txt}{c |} {com}. spearman laborhrs rooms volume, stats(rho obs p) {txt}{c TLC }{hline 17}{c TRC} {c |} Key{col 19}{c |} {c LT }{hline 17}{c RT} {c |} {it: rho}{col 19}{c |} {c |} {it: Number of obs}{col 19}{c |} {c |} {it: Sig. level}{col 19}{c |} {c BLC }{hline 17}{c BRC} {c |} laborhrs rooms volume {hline 13}{c +}{hline 27} laborhrs {c |} {res} 1.0000 {txt}{c |} {res} 36 {txt}{c |} {c |} rooms {c |} {res} 0.8884 1.0000 {txt}{c |} {res} 36 36 {txt}{c |} {res} 0.0000 {txt}{c |} volume {c |} {res} 0.9292 0.9526 1.0000 {txt}{c |} {res} 36 36 36 {txt}{c |} {res} 0.0000 0.0000 {txt}{c |} {com}. . /* Clearly, labor hours is very strongly correlated with both predictors, and > rank transformation of outcome and predictor has minimal effects on the > pairwise correlations. Also the two predictors are highly correlated (i.e., > collinear), which would complicate fitting and interpreting models including > both. > > Now Fit a regression predicting labor hours using each of the others one at at > time. */ . . foreach x in rooms volume {c -(} {txt} 2{com}. regress laborhrs `x' {txt} 3{com}. {c )-} {txt} Source {c |} SS df MS Number of obs ={res} 36 {txt}{hline 13}{c +}{hline 34} F(1, 34) = {res} 99.97 {txt} Model {c |} {res} 5799.19478 1 5799.19478 {txt}Prob > F ={res} 0.0000 {txt} Residual {c |} {res} 1972.24272 34 58.0071388 {txt}R-squared ={res} 0.7462 {txt}{hline 13}{c +}{hline 34} Adj R-squared ={res} 0.7388 {txt} Total {c |} {res} 7771.4375 35 222.041071 {txt}Root MSE = {res} 7.6162 {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {col 1} laborhrs{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {space 7}rooms {c |}{col 14}{res}{space 2} 11.13846{col 26}{space 2} 1.113992{col 37}{space 1} 10.00{col 46}{space 3}0.000{col 54}{space 4} 8.874559{col 67}{space 3} 13.40237 {txt}{space 7}_cons {c |}{col 14}{res}{space 2}-10.18099{col 26}{space 2} 4.115117{col 37}{space 1} -2.47{col 46}{space 3}0.019{col 54}{space 4}-18.54392{col 67}{space 3}-1.818067 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {res} {txt} Source {c |} SS df MS Number of obs ={res} 36 {txt}{hline 13}{c +}{hline 34} F(1, 34) = {res} 272.99 {txt} Model {c |} {res} 6910.71887 1 6910.71887 {txt}Prob > F ={res} 0.0000 {txt} Residual {c |} {res} 860.718633 34 25.3152539 {txt}R-squared ={res} 0.8892 {txt}{hline 13}{c +}{hline 34} Adj R-squared ={res} 0.8860 {txt} Total {c |} {res} 7771.4375 35 222.041071 {txt}Root MSE = {res} 5.0314 {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {col 1} laborhrs{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {space 6}volume {c |}{col 14}{res}{space 2} .0500803{col 26}{space 2} .0030311{col 37}{space 1} 16.52{col 46}{space 3}0.000{col 54}{space 4} .0439204{col 67}{space 3} .0562402 {txt}{space 7}_cons {c |}{col 14}{res}{space 2} -2.36966{col 26}{space 2} 2.073261{col 37}{space 1} -1.14{col 46}{space 3}0.261{col 54}{space 4}-6.583033{col 67}{space 3} 1.843713 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {res}{txt} {com}. . /* Look closely at each model output and consider which model is better. > > First, R-squared is high for both models, but considerably higher for the model > using volume to predict labor hours. Recall that R-squared is the square of the > Pearson correlations we calculated earlier: */ . . foreach x in rooms volume {c -(} {txt} 2{com}. qui corr laborhrs `x' {txt} 3{com}. di as result "`=`r(rho)'^2'" {txt} 4{com}. {c )-} {res}.746219059183119 .8892458913514151 {txt} {com}. . /* Now describe the model from the equation for labor hours as predicted by the > volumne of items to be moved. By typing the regress command without arguments, > we get back the results of the most recently estimated model: */ . . regress {txt} Source {c |} SS df MS Number of obs ={res} 36 {txt}{hline 13}{c +}{hline 34} F(1, 34) = {res} 272.99 {txt} Model {c |} {res} 6910.71887 1 6910.71887 {txt}Prob > F ={res} 0.0000 {txt} Residual {c |} {res} 860.718633 34 25.3152539 {txt}R-squared ={res} 0.8892 {txt}{hline 13}{c +}{hline 34} Adj R-squared ={res} 0.8860 {txt} Total {c |} {res} 7771.4375 35 222.041071 {txt}Root MSE = {res} 5.0314 {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {col 1} laborhrs{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {space 6}volume {c |}{col 14}{res}{space 2} .0500803{col 26}{space 2} .0030311{col 37}{space 1} 16.52{col 46}{space 3}0.000{col 54}{space 4} .0439204{col 67}{space 3} .0562402 {txt}{space 7}_cons {c |}{col 14}{res}{space 2} -2.36966{col 26}{space 2} 2.073261{col 37}{space 1} -1.14{col 46}{space 3}0.261{col 54}{space 4}-6.583033{col 67}{space 3} 1.843713 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {com}. . /* The model shows that expected labor hours increase by about 0.05 hours (95% > confidence interval 0.043, 0.056, p<0.0005) for each additional cubic foot of > items to be moved. The standard error of the estimate, which is used in > calculating the t-statistic, the p-value, and the confidence interval, is > 0.003. > > The overall F-statistic for the model (in this case, with a single predictor, > equal to the the squuare of the t-statistic for volume) is ~273, and again, > highly statistically significant (P<0.00005). > > Notably, the intercept is not-statistically significant, and thus statistically > indistinguishable from zero. This may suggest that the essentially none the > labor hours are used in doing anything other than moving items; it might be > positive if labor hours also included ancillary tasks such as getting to and > from the job. > > Finally, we can use the model to predict the labor hours for a job involving > 1200 cubic feet of items: */ . . lincom _cons + 1200*volume {p 0 7}{space 1}{text:( 1)}{space 1} {res}1200{res}*{res}volume + _cons = 0{p_end} {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {col 1} laborhrs{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {space 9}(1) {c |}{col 14}{res}{space 2} 57.72667{col 26}{space 2} 1.932593{col 37}{space 1} 29.87{col 46}{space 3}0.000{col 54}{space 4} 53.79917{col 67}{space 3} 61.65417 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {com}. . /* So the predicted labor hours for moving 1200 cubic feet of items is 57.7 > hours (95% CI 53.8, 61.7 hours). > > Returning to the issue of non-linearity, which was not very evident in the > Lowess smooth of labor hours on volume: we can check this assumption by adding > a quadratic term to the model. */ . . cap drop volume2 {txt} {com}. qui gen volume2 = volume^2 {txt} {com}. regress laborhrs volume volume2 {txt} Source {c |} SS df MS Number of obs ={res} 36 {txt}{hline 13}{c +}{hline 34} F(2, 33) = {res} 172.91 {txt} Model {c |} {res} 7094.43363 2 3547.21682 {txt}Prob > F ={res} 0.0000 {txt} Residual {c |} {res} 677.003867 33 20.5152687 {txt}R-squared ={res} 0.9129 {txt}{hline 13}{c +}{hline 34} Adj R-squared ={res} 0.9076 {txt} Total {c |} {res} 7771.4375 35 222.041071 {txt}Root MSE = {res} 4.5294 {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {col 1} laborhrs{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {space 6}volume {c |}{col 14}{res}{space 2} .0148792{col 26}{space 2} .0120755{col 37}{space 1} 1.23{col 46}{space 3}0.227{col 54}{space 4}-.0096885{col 67}{space 3} .0394469 {txt}{space 5}volume2 {c |}{col 14}{res}{space 2} .0000237{col 26}{space 2} 7.91e-06{col 37}{space 1} 2.99{col 46}{space 3}0.005{col 54}{space 4} 7.58e-06{col 67}{space 3} .0000398 {txt}{space 7}_cons {c |}{col 14}{res}{space 2} 8.578305{col 26}{space 2} 4.107046{col 37}{space 1} 2.09{col 46}{space 3}0.045{col 54}{space 4} .2224565{col 67}{space 3} 16.93415 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {res}{txt} {com}. . /* The quadratic term captures any non-linearity in the relationship of volume > to labor hours, and is statistically significant (P=0.005). Also R-squared > increases a bit, from .8882 to .9129. The coefficients for volume in this > model are not easy to interpret. That said, with this slightly better fitting > model, the constant term is positive and also statistically significant, > suggesting that there may be some fixed labor costs independent of the volume > of items. In addition, we can predict the labor hours for that 1200 cubic foot > job. Note that we have to use macro syntax to calculate the square of 1200 > within the lincom command, which won't otherwise calculate it on the fly. */ . . lincom _cons + 1200*volume + `=1200^2'*volume2 {p 0 7}{space 1}{text:( 1)}{space 1} {res}1200{res}*{res}volume + 1440000{res}*{res}volume2 + _cons = 0{p_end} {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {col 1} laborhrs{col 14}{c |} Coef.{col 26} Std. Err.{col 38} t{col 46} P>|t|{col 54} [95% Con{col 67}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {space 9}(1) {c |}{col 14}{res}{space 2} 60.51209{col 26}{space 2} 1.973104{col 37}{space 1} 30.67{col 46}{space 3}0.000{col 54}{space 4} 56.49778{col 67}{space 3} 64.5264 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12} {com}. . /* So this model predicts 60.5 labor hours (95% CI 56.5-64.5) for the 1200 > cubic foot job. > > Finally, check the model for outliers. */ . . predict resid, resid {txt} {com}. graph box resid, name(box_resid, replace) {res}{txt} {com}. qnorm resid, name(qq_resid, replace) {res}{txt} {com}. . /* No reason for concern here. In Biostat 208, you will learn to check for > "influential" points: i.e., observations that change the regression coefficients > by meaningful amounts, and which usually represent big residuals in combination > with high-leverage predictor values in the tails of the predictor distribution. */ . . log close {txt}name: {res} {txt}log: {res}/Users/eric/Work/MB/ATCR/200/Labs/lab 9.smcl {txt}log type: {res}smcl {txt}closed on: {res}15 Oct 2018, 08:36:27 {txt}{.-} {smcl} {txt}{sf}{ul off}