lines='obs_number,low_bw,age,weight,race,smoked,n_premature,hypertension,uterine_irritability,n_visits,birthweight_gm 4,1,28,120,3,1,1,0,1,0, 709 11,1,34,187,2,1,0,1,0,0,1135 15,1,25, 85,3,0,0,0,1,0,1474 17,1,23, 97,3,0,0,0,1,1,1588 19,1,24,132,3,0,0,1,0,0,1729 22,1,32,105,1,1,0,0,0,0,1818 24,1,25,115,3,0,0,0,0,0,1893 26,1,25, 92,1,1,0,0,0,0,1928 28,1,21,200,2,0,0,0,1,2,1928 30,1,21,103,3,0,0,0,0,0,1970 32,1,25, 89,3,0,2,0,0,1,2055 34,1,19,112,1,1,0,0,1,0,2084 36,1,24,138,1,0,0,0,0,0,2100 40,1,20,120,2,1,0,0,0,3,2126 43,1,27,130,2,0,0,0,1,0,2187 45,1,17,110,1,1,0,0,0,0,2225 47,1,20,109,3,0,0,0,0,0,2240 50,1,18,110,2,1,1,0,0,0,2296 52,1,21,100,3,0,1,0,0,4,2301 56,1,31,102,1,1,1,0,0,1,2353 59,1,23,187,2,1,0,0,0,1,2367 61,1,24,105,2,1,0,0,0,0,2381 63,1,23,120,3,0,0,0,0,0,2410 67,1,22,130,1,1,0,0,0,1,2410 69,1,23,110,1,1,1,0,0,0,2424 75,1,26,154,3,0,1,1,0,1,2442 77,1,26,190,1,1,0,0,0,0,2466 79,1,28, 95,1,1,0,0,0,2,2466 82,1,23, 94,3,1,0,0,0,0,2495 84,1,21,130,1,1,0,1,0,3,2495 86,0,33,155,3,0,0,0,0,3,2551 88,0,21,108,1,1,0,0,1,2,2594 91,0,21,124,3,0,0,0,0,0,2622 93,0,17,103,3,0,0,0,0,1,2637 95,0,26,113,1,1,0,0,0,0,2665 97,0,19,150,3,0,0,0,0,1,2733 99,0,30,107,3,0,1,0,1,2,2750 101,0,18,100,1,1,0,0,0,0,2769 103,0,25,118,1,1,0,0,0,3,2782 10,1,29,130,1,0,0,0,1,2,1021 13,1,25,105,3,0,1,1,0,0,1330 16,1,27,150,3,0,0,0,0,0,1588 18,1,24,128,2,0,1,0,0,1,1701 20,1,21,165,1,1,0,1,0,1,1790 23,1,19, 91,1,1,2,0,1,0,1885 25,1,16,130,3,0,0,0,0,1,1899 27,1,20,150,1,1,0,0,0,2,1928 29,1,24,155,1,1,1,0,0,0,1936 31,1,20,125,3,0,0,0,1,0,2055 33,1,19,102,1,0,0,0,0,2,2082 35,1,26,117,1,1,1,0,0,0,2084 37,1,17,130,3,1,1,0,1,0,2125 42,1,22,130,1,1,1,0,1,1,2187 44,1,20, 80,3,1,0,0,1,0,2211 46,1,25,105,3,0,1,0,0,1,2240 49,1,18,148,3,0,0,0,0,0,2282 51,1,20,121,1,1,1,0,1,0,2296 54,1,26, 96,3,0,0,0,0,0,2325 57,1,15,110,1,0,0,0,0,0,2353 60,1,20,122,2,1,0,0,0,0,2381 62,1,15,115,3,0,0,0,1,0,2381 65,1,30,142,1,1,1,0,0,0,2410 68,1,17,120,1,1,0,0,0,3,2414 71,1,17,120,2,0,0,0,0,2,2438 76,1,20,105,3,0,0,0,0,3,2450 78,1,14,101,3,1,1,0,0,0,2466 81,1,14,100,3,0,0,0,0,2,2495 83,1,17,142,2,0,0,1,0,0,2495 85,0,19,182,2,0,0,0,1,0,2523 87,0,20,105,1,1,0,0,0,1,2557 89,0,18,107,1,1,0,0,1,0,2600 92,0,22,118,1,0,0,0,0,1,2637 94,0,29,123,1,1,0,0,0,1,2663 96,0,19, 95,3,0,0,0,0,0,2722 98,0,22, 95,3,0,0,1,0,0,2751 100,0,18,100,1,1,0,0,0,0,2769 102,0,15, 98,2,0,0,0,0,0,2778 104,0,20,120,3,0,0,0,1,0,2807 105,0,28,120,1,1,0,0,0,1,2821 107,0,31,100,1,0,0,0,1,3,2835 109,0,28,120,3,0,0,0,0,0,2863 112,0,28,167,1,0,0,0,0,0,2877 114,0,29,150,1,0,0,0,0,2,2920 116,0,17,113,2,0,0,0,0,1,2920 118,0,24, 90,1,1,1,0,0,1,2948 120,0,25,155,1,0,0,0,0,1,2977 123,0,29,140,1,1,0,0,0,2,2977 125,0,27,124,1,1,0,0,0,0,2922 127,0,33,109,1,1,0,0,0,1,3033 129,0,19,189,1,0,0,0,0,2,3062 131,0,21,160,1,0,0,0,0,0,3062 133,0,18, 90,1,1,0,0,1,0,3062 135,0,19,132,3,0,0,0,0,0,3090 137,0,22, 85,3,1,0,0,0,0,3090 139,0,23,128,3,0,0,0,0,0,3104 141,0,30, 95,1,1,0,0,0,2,3147 143,0,16,110,3,0,0,0,0,0,3175 145,0,30,153,3,0,0,0,0,0,3203 147,0,17,119,3,0,0,0,0,0,3225 149,0,23,119,3,0,0,0,0,2,3232 151,0,28,140,1,0,0,0,0,0,3234 155,0,20,169,3,0,1,0,1,1,3274 159,0,28,250,3,1,0,0,0,6,3303 161,0,22,158,2,0,1,0,0,2,3317 163,0,31,150,3,1,0,0,0,2,3321 166,0,16,112,2,0,0,0,0,0,3374 168,0,18,229,2,0,0,0,0,0,3402 170,0,32,134,1,1,1,0,0,4,3430 173,0,23,190,1,0,0,0,0,0,3459 175,0,32,170,1,0,0,0,0,0,3473 177,0,20,127,3,0,0,0,0,0,3487 180,0,17,120,3,1,0,0,0,0,3572 182,0,23,130,1,0,0,0,0,0,3586 184,0,22,125,1,0,0,0,0,1,3614 186,0,21,134,3,0,0,0,0,2,3629 188,0,25, 95,1,1,3,0,1,0,3637 190,0,29,135,1,0,0,0,0,1,3651 192,0,19,147,1,1,0,0,0,0,3651 195,0,30,137,1,0,0,0,0,1,3699 197,0,19,184,1,1,0,1,0,0,3756 200,0,23,110,1,0,0,0,0,1,3770 202,0,25,241,2,0,0,1,0,0,3790 204,0,22,169,1,0,0,0,0,0,3827 206,0,16,170,2,0,0,0,0,4,3860 208,0,18,120,3,0,0,0,0,1,3884 210,0,33,117,1,0,0,0,1,1,3912 212,0,28,134,3,0,0,0,0,1,3941 106,0,32,121,3,0,0,0,0,2,2835 108,0,36,202,1,0,0,0,0,1,2836 111,0,25,120,3,0,0,0,1,2,2877 113,0,17,122,1,1,0,0,0,0,2906 115,0,26,168,2,1,0,0,0,0,2920 117,0,17,113,2,0,0,0,0,1,2920 119,0,35,121,2,1,1,0,0,1,2948 121,0,25,125,2,0,0,0,0,0,2977 124,0,19,138,1,1,0,0,0,2,2977 126,0,31,215,1,1,0,0,0,2,3005 128,0,21,185,2,1,0,0,0,2,3042 130,0,23,130,2,0,0,0,0,1,3062 132,0,18, 90,1,1,0,0,1,0,3062 34,0,32,132,1,0,0,0,0,4,3080 136,0,24,115,1,0,0,0,0,2,3090 138,0,22,120,1,0,0,1,0,1,3100 140,0,22,130,1,1,0,0,0,0,3132 42,0,19,115,3,0,0,0,0,0,3175 144,0,21,110,3,1,0,0,1,0,3203 146,0,20,103,3,0,0,0,0,0,3203 148,0,17,119,3,0,0,0,0,0,3225 150,0,24,110,3,0,0,0,0,0,3232 154,0,26,133,3,1,2,0,0,0,3260 56,0,24,115,3,0,0,0,0,2,3274 160,0,20,141,1,0,2,0,1,1,3317 162,0,22,112,1,1,2,0,0,0,3317 164,0,23,115,3,1,0,0,0,1,3331 167,0,16,135,1,1,0,0,0,0,3374 169,0,25,140,1,0,0,0,0,1,3416 172,0,20,121,2,1,0,0,0,0,3444 174,0,22,131,1,0,0,0,0,1,3460 176,0,30,110,3,0,0,0,0,0,3544 179,0,23,123,3,0,0,0,0,0,3544 181,0,19,105,3,0,0,0,0,0,3572 183,0,36,175,1,0,0,0,0,0,3600 185,0,24,133,1,0,0,0,0,0,3614 187,0,19,235,1,1,0,1,0,0,3629 189,0,16,135,1,1,0,0,0,0,3643 191,0,29,154,1,0,0,0,0,1,3651 193,0,19,147,1,1,0,0,0,0,3651 196,0,24,110,1,0,0,0,0,1,3728 199,0,24,110,3,0,1,0,0,0,3770 201,0,20,120,3,0,0,0,0,0,3770 203,0,30,112,1,0,0,0,0,1,3799 205,0,18,120,1,1,0,0,0,2,3856 207,0,32,186,1,0,0,0,0,2,3860 209,0,29,130,1,1,0,0,0,2,3884 211,0,20,170,1,1,0,0,0,0,3940 213,0,14,135,1,0,0,0,0,0,3941 214,0,28,130,3,0,0,0,0,0,3969 216,0,16, 95,3,0,0,0,0,1,3997 218,0,26,160,3,0,0,0,0,0,4054 220,0,22,129,1,0,0,0,0,0,4111 222,0,31,120,1,0,0,0,0,2,4167 224,0,19,120,1,1,0,0,0,0,4238 226,0,45,123,1,0,0,0,0,1,4990 215,0,25,120,1,0,0,0,0,2,3983 217,0,20,158,1,0,0,0,0,1,3997 219,0,21,115,1,0,0,0,0,1,4054 221,0,25,130,1,0,0,0,0,2,4153 223,0,35,170,1,0,1,0,0,1,4174 225,0,24,116,1,0,0,0,0,1,4593' con = textConnection( lines ) DF = read.table( con, header=TRUE, sep=',' ) close(con) # Part (a): # #postscript("../../WriteUp/Graphics/Chapter10/ex_3_two_densities.eps", onefile=FALSE, horizontal=FALSE) par(mfrow=c(2,1)) plot( density( DF$birthweight_gm, adjust=1.0 ), main='adjust=1' ) plot( density( DF$birthweight_gm, adjust=0.6 ), main='adjust=0.6' ) # gives a long left tail with a hump to the left of the main hump par(mfrow=c(1,1)) #dev.off() #postscript("../../WriteUp/Graphics/Chapter10/ex_3_densities_by_BW.eps", onefile=FALSE, horizontal=FALSE) lbw = DF$low_bw == 1 nbw = DF$low_bw != 1 plot( density( DF[lbw,]$birthweight_gm ), col='red', xlim=range(DF$birthweight_gm), main='' ) lines( density( DF[nbw,]$birthweight_gm ), col='green', main='' ) legend( 3750, 0.0012, legend=c("low BW", "normal BW"), col=c('red', 'green'), lty=c(1,1) ) grid() #dev.off() # For prediction we want to look at which variables are the most correlated with LB: # (dropping the one obviously not important) # df = DF df$obs_number = NULL df$birthweight_gm = NULL c = cor( df ) row = c[,1] print( t( row[ order(abs(row), decreasing=TRUE), drop=FALSE ] ) )