program define showls version 4.0 preserve clear global D_sm11 " This lab illustrates the idea of fitting a least squares line to a scatterplot. When you click on Run, the lab picks a sample of size n (you can pick n) from a bivariate population having x's between -1 and 1. The population has a true line having intercept 0 and a slope that you can pick. You can also pick how close the points are to the true line by specifying a desired r^2 (small value means points far from line). Then the lab draws the scatterplot and least squares line (with the vertical distances of the points to the line drawn) and the true line." wdctl static D_sm11 5 5 290 55 global D_sm12 " You can also specify how many times you want this to be done, that is, the # of samples. If you pick more than 1, the lab will only draw the least squares line each time as well as the true line." wdctl static D_sm12 5 65 290 25 global D_sm13 " Things to notice: 1) The more noise there is, the farther the least squares line is from the true line on average. 2) The larger n is, the closer the least squares line is to the true line on the average." wdctl static D_sm13 5 95 200 50 global D_sm1 "n" global HJN_V1=20 wdctl static D_sm1 120 160 8 10 wdctl edit HJN_V1 130 160 20 10 global D_sm8 "# of Samples" global D_sm9 "1 20 100" wdctl static D_sm8 160 130 60 10 global D_var2 "1" wdctl ssimple D_var2 D_sm9 160 145 55 40 global D_sm6 "slope" global D_sm7 "3.0 2.0 1.0 0.0 -1. -2. -3." wdctl static D_sm6 230 100 20 10 global D_var1 "3.0" wdctl ssimple D_var1 D_sm7 230 115 20 70 global D_sm3 "Desired r^2" global D_sm4 "10 25 50 75 90 99" wdctl static D_sm3 260 90 30 20 global D_var3 "50" wdctl ssimple D_var3 D_sm4 260 115 30 70 wdctl button "Run" 5 160 30 14 D_b1 wdctl button "Close" 40 160 30 14 D_b2 wdctl button "Help" 75 160 30 14 D_b3 help * showlsdr global D_b1 "showlsdr" global D_b2 "exit 1234" global D_b3 "whelp showls" cap noi wdlg "Least Squares" $D_dlgx $D_dlgy 300 240 restore end program define showlsdr version 4.0 gph open gph pen 1 local n=$HJN_V1 if `n' < 5 | `n' > 100 { sstopbox stop "n must be between 5 and 100" exit} if "$D_var1"=="3.0" {local slope=3.} if "$D_var1"=="2.0" {local slope=2.} if "$D_var1"=="1.0" {local slope=1.0} if "$D_var1"=="0.0" {local slope=0.0} if "$D_var1"=="-1." {local slope=-1.} if "$D_var1"=="-2." {local slope=-2.} if "$D_var1"=="-3." {local slope=-3.} if "$D_var2"=="1" {local ns=1} if "$D_var2"=="20" {local ns=20} if "$D_var2"=="100" {local ns=100} if "$D_var3"=="10" {local sn=.10} if "$D_var3"=="25" {local sn=.25} if "$D_var3"=="50" {local sn=.50} if "$D_var3"=="75" {local sn=.75} if "$D_var3"=="90" {local sn=.90} if "$D_var3"=="99" {local sn=.99} local sn=sqrt(`sn') set obs `n' tempvar x y z gen `x'=2*(((_n-1)/(_N-1))-.5) gen `y'=12*(uniform()-.5) gen `z'=. graph `y' `x', s(i) xlab(-1,-.5,0,.5,1) ylab(-6,-4,-2,0,2,4,6) /* */ l1(" ") l2(" ") b1(" ") b2(" ") bbox(3000,0,23000,32000,850,400,0) gphconv if `slope' == 0. { local sig=1-`sn'^2} /* else { local sig=2*`sn'*abs(`slope')/2} */ else { summarize `x' local sig=abs(`slope')*sqrt(_result(4)*(1-`sn'^2))/`sn'} local i=1 while `i' <= `ns' { replace `y'=`slope'*`x'+`sig'*invnorm(uniform()) regress `y' `x' drop `z' tempvar z predict `z' lines `x' `z' gph pen 1 points `x' `y' gph pen 2 if `ns' == 1 { lsfit `x' `y' local msl: display %5.2f $slope local mint: display %5.2f $int local mcorr: display %5.2f 100*$corr^2 gph text 2000 7000 0 -1 LS slope, intercept: `msl',`mint', Actual r^2=`mcorr' * points `x' `y' segments `x' `y' `x' `z'} local i=`i'+1 } gph pen 3 replace `y'=`slope'*`x' lines `x' `y' local int=0 local msl1: display %5.2f `slope' local mint1: display %5.2f `int' local msn=100*`sn'^2 local msn: display %5.2f `msn' gph text 1000 7000 0 -1 True slope, intercept: `msl1',`mint1', n=`n', Desired r^2=`msn' gph close end exit