Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation – GOF.

Similar presentations


Presentation on theme: "Today Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation – GOF."— Presentation transcript:

1 Today Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation – GOF – Coefficients – Checking assumptions

2 Stata file types.ado – programs that add commands to Stata.do – Batch files that execute a set of Stata commands.dta – Data file in Stata’s format.log – Output saved as plain text by the log using command

3 The working directory The working directory is the default directory for any file operations such as using & saving data, or logging output cd “d:\my work\”

4 Saving output to log files Syntax for the log command –log using filename [, append replace [smcl|text]] To close a log file –log close

5 Using and saving datasets Load a Stata dataset – use d:\myproject\data.dta, clear Save – save d:\myproject\data, replace Using change directory – cd d:\myproject – Use data, clear – save data, replace

6 Entering data Data in other formats – You can use SPSS to convert data – You can use the infile and insheet commands to import data in ASCII format Entering data by hand – Type edit or just click on the data-editor button

7 Do-files You can create a text file that contains a series of commands Use the do-editor to work with do-files Example I

8 Adding comments // or * denote comments stata should ignore Stata ignores whatever follows after /// and treats the next line as a continuation Example II

9 A recommended structure //if a log file is open, close it capture log close //dont'pause when output scrolls off the page set more off //change directory to your working directory cd d:\myproject //log results to file myfile.log log using myfile, replace text // * myfile.do-written 7 feb 2010 to illustrate do-files // your commands here //close the log file log close

10 Serious data analysis Ensure replicability use do+log files Document your do-files – What is obvious today, is baffling in six months Keep a research log – Diary that includes a description of every program you run Develop a system for naming files

11 Serious data analysis New variables should be given new names Use labels and notes Double check every new variable ARCHIVE

12 The Stata syntax Regress y x1 x2 if x3 <20, cluster(x4) 1.Regress = Command – What action do you want to performed 2.y x1 x2 = Names of variables, files or other objects – On what things is the command performed 3.if x3 <20 = Qualifier on observations – On which observations should the command be performed 4., cluster(x4) = Options – What special things should be done in executing the command

13 Examples tabulate smoking race if agemother > 30, row Example of the if qualifier – sum agemother if smoking == 1 & weightmother < 100

14 Elements used for logical statements OperatorDefinitionExample ==Equal toIf male == 1 !=Not equal toIf male !=1 >Greater thanIf age > 20 >=Greater than or equal toIf age >=21 <Less thanIf age<66 <=Less than or equal toIf age<=65 &AndIf age==21&male ==1 |orIf age =65

15 Missing values Automatically excluded when Stata fits models, they are stored as the largest positive values Beware – The expression ‘age > 65’ can thus also include missing values – To be sure type: ‘age > 65 & age !=.’

16 Selecting observations drop variable list Keep variable list drop if age < 65

17 Creating new variables generate command – generate age2 = age * age – generate – see help function – !!sometimes the command egen is a useful alternative, f.i. – egen meanage = mean(age)

18 Useful functions FunctionDefinitionExample +additiongen y = a+b -subtractiongen y = a-b /Divisiongen density=population/area *Multiplicationgen y = a*b ^Take to a powergen y = a^3 lnNatural loggen lnwage = ln(wage) expexponentialgen y = exp(b) sqrtSquare rootGen agesqrt = sqrt(age)

19 Replace command replace has the same syntax as generate but is used to change values of a variable that already exists gen age_dum =. replace age = 0 if age < 5 replace age = 1 if age >=5

20 Recode Change values of exisiting variables – Change 1 to 2 and 3 to 4: recode origvar (1=2)(3=4), gen(myvar1) – Change missings to 1: recode origvar (.=1), gen(origvar)

21 Logistic regression Lets use a set of data collected by the state of California from 1200 high schools measuring academic achievement. Our dependent variable is called hiqual. Our predictor variable will be a continuous variable called avg_ed, which is a continuous measure of the average education (ranging from 1 to 5) of the parents of the students in the participating high schools.

22 OLS in Stata

23

24 Logistic regression in Stata

25

26 Multiple predictors

27 Model fit: the likelihood ratio test

28 Model fit: LR test

29 Pseudo R2: proportional change in LL

30 Classification Table

31

32 Interpreting coefficients: significance

33 Comparing models

34 After the full model and storage, estimate nested model

35 Likelihood ratio test

36 Interpretation of coefficients: direction

37

38 Interpretation of coefficients: Magnitude

39

40 the assumptions of logistic regression The true conditional probabilities are a logistic function of the independent variables. No important variables are omitted. No extraneous variables are included. The independent variables are measured without error. The observations are independent. The independent variables are not linear combinations of each other.

41 Hosmer & Lemeshow Test divides sample in subgroups, checks whether difference between observed and predicted is about equal in these groups Test should not be significant (indicating no difference)

42 Goodness of fit: Hosmer & Lemeshow Average Probability In j th group

43 First logistic regression

44 Then postestimation command

45 Specification error

46 Including interaction term helps

47 Ok now

48

49 Multicollinearity

50 Influential observations

51

52

53 To do Perform a logistic regression analysis Use apilog.dta Awards = dependent variable


Download ppt "Today Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation – GOF."

Similar presentations


Ads by Google