/*---------------------------------------------------
 HOW TO USE THIS FILE
 This is an HTML version of the Stata do file ex2.do
 It is intended to show you the code and to allow links not to use interactively
use ex2.do in the data editor for this
 EVERYTHING INSIDE (STAR SLASH)  AND (SLASH STAR)
 IS TAKEN AS A COMMENT These are shown in black here
---------------------------------------------------------*/
To see output from the commands go to the Stata results.

Links in this page

Setting up a survey design
Simple means and proportions Table 2.3
Effect of design aspects on precision of estimates table 2.4
Chi square tests tables 2.6 onwards
Internet use by council area
Survey logistic regressions
Analyse  SHS data in Stata Exemplar 2
First read in the file

back to top uparrow

set the survey description
and run the svydes to get a description of it
--------------------------------------------------*/
svyset [pwei=ind_wt],psu(psu) strata(stratum)
svydes

/*-------------------------------------------------
First simple proportions of INTERNET USE

 RESULTS FOR TABLE 2.3  IN EXEMPLAR 2 HOME PAGE
back to top uparrow

We give the code for this below, but in Stata you can
Also get a dialogue box by searching for the commands in
the help menu. When you complete this the code will be
generated for you.

You can also reach the dialogue box from the statistics menu
under survey data analysis. When you press OK you will see
the commands echoed before the results.

The same is true for all the commands below.
The dialogue boxes have handy links to bits of the help menu
-----------------------------------------------------*/


svyprop intuse
/*------------------------------------------------------
 svyprop does not give design effects so must treat proportion as
  the mean of a 0/1 variable to get the design effects

and we can also get them for subgroups
 -------------------------------------------------------------*/
svymean intuse , ci deff
svymean intuse, deff deft by(sex)

/*------------------------------------------------------------
Now getting proportions of internet hours use for users

--------------------------------------------------------------*/
svymean rc5
svymean rc5,  by (intuse)
/*------------------------------------------------------------
this fails because of lonely PSUs due to missing values
------------------------------------------------------------*/
svydes if intuse==1
/*---------------------------------------------------
This identifies them but there is 
no easy way round it except by setting it up as a regression. -------------------------------------------------------*/
/*-------------------------------------------------
 RESULTS FOR TABLE 2.4  IN EXEMPLAR 2 HOME PAGE
 different design options
back to top uparrow


In order to text out what the design effect for internet
use would have been for different types of design it
is necessary to set up a new design
BUT all previous settings need to be cleared as
part of this process.
Unless this is done previous settings will remain
-------------------------------------------------------------*/

svyset [pweight=ind_wt],  clear( strata psu pweight )
svymean intuse , ci deff
svyset ,psu(psu)
svymean intuse , ci deff
svyset, clear(psu) strata(stratum)
svymean intuse , ci deff
svyset,psu(psu)
/* ---------------------------------------------------------
Survey set-up is now back to what it should be

back to top uparrow

Now chi square tests for surveys
So the table with RC5 fails for same reasons
s previously.

------------------------------------------------------------*/

svytab sex intuse,count row percent
svytab rc5 intuse, count row percent
svytab sex groc, count row percent format(%10.2f)

/*-------------------------------------------------------------

back to top uparrow

Now internet use by council area to illustrate
different types of design effect for sub-groups

The srssubpop option in the second command gives
design effects compared to simple random sampling
in sub populations
-----------------------------------------------------------*/

 svymean intuse,  ci deff by(council)
svymean intuse,  ci deff by(council) srssubpop


/*--------------------------------------------------------

 back to top uparrow

now some logistic regression
first get the dummy variables
-----------------------------------------------------------*/
tabulate groupinc, generate(groupinc)
tabulate groupinc1
/*----------------------------------
will miss out groupinc2 as analysis restricted to
cases with income data and the first dummy
variables gives missing values
-----------------------------------------*/
svylogit intuse  groupinc3-groupinc6 if groupinc>0,prob deff deft or
/*----------------------------------------------
compare with unweighted logistic regression
--------------------------------------------------*/
logistic intuse  groupinc3-groupinc6 if groupinc>0, or

/*------------------------------------------------
now add urban rural you will see almost no effect
-----------------------------------------------------*/
tabulate shs_6cla, generate (rural)
svylogit intuse groupinc3-groupinc6 rural2-rural6 if groupinc>0,prob or

/*--------------------------------------------------
to fit a spline model you need to install the bsplines
package which is done easily by following the help
for bsplines
you need to be connected to the internet for this to work
--------------------------------------------------------*/
 bspline,x(age)  power(3) gen(bs)

svylogit intuse bs1-bs4,noconst deft deff
predict pr0 pr1
plot pr1 age