



Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!




If we need to do similar analyses over and over, we want to keep a record of what we did Also helps you to collaborate with others Easier to debug when we run into errors STATA datasets are saved as “.dta” files, opening them is very easy (we will also learn to open in Stata other types of files (.xls, .csv, .txt)) To open one : window —> do-file editor —> new do-file editor Open data file: on the menu file —> open / manually: type use in the command bar To look at our data type br Describe command It helps you understand the structure of your data before starting any analysis Summarize command provides descriptive statistics for variables (^) Tabulate command creates frequency tables, that is it summarizes how many observations fall into each category of a variable (or combination of variables) Clear all previous data Correlation between two variables —> corr reddito_medio furti_app <— example Graphic scatter plot (grafico a dispersione) twoway ( scatter variable1 variable2) (Lfit v1 v2) crea linea retta First variable asse Y second one asse X (^) In Stata , the corr (or correlate) command computes correlation coefficients between variables [ Pearson correlation coefficient ] (^) It measures strength and direction of a linear relationship (^) The correlation ranges from –1 to +1, 0 means no linear relationship Keeping a log file is useful to: Look back at interesting results Track changes made during the analysis Log files contain: The commands you run The output produced by Stata The log file can be read using any word processor Log using mylog.log, name (…) Se sbagli e lo rifai alla fine metti replace
TRUE or FALSE: Stata cannot be used interactively and can only be used through coding. FALSE (^) Stata data files have the .dta extension. TRUE Do-files store commands and results. FALSE The average number of apartment burglaries (furti_app) across Italian municipalities is higher than the average number of robberies (rapine). TRUE LECTURE Stata data files are .dta files data come in other formats. .xls (excel) .csv .txt .dat XLS common data format. File - Import - Excel Spreadsheet Note that you need to indicate which sheet to import CSV can store lots of data If the data is already in CSV no need to change to import File - Import - Text Data Check the options! Delimiter First row for variable names Importing delimited .txt data works the same way Potential problems : Variable names: Stata does not accept variable names starting with numbers (among other rules) Solution —> Change it before in excel (one trick is to include just a letter before the numbers: 1998 —> y1998) Characters: Importing string data with latin characters - play with the enconding Missing values: Stata imported numeric data as strings because of some missing values Solution —> use the command destring maybe with option force Size: Data is too big (because of storage type) —> use compress After you have imported the data it is useful to save in .dta You can use the menu: File - Save (as) Or just use the command save —> save [file_name.dta] If the file already exists and you want to overwrite it add replace save [file_name.dta], replace Logical expressions (^) & → and (^) | → or (^)! → not (^) >= → greater or equal (^) <= → smaller or equal (^) == → equal (^) != → not equal Mathematical functions (^) abs(x) → absolute value (^) log(x) → natural logarithm (base e ) (^) sqrt(x) → square root (^) exp(x) → exponential
use preserve and restore to temporarily save and recover the dataset In Stata, the preserve and restore commands are used together to temporarily save and then recover the dataset in memory When your datasets are related, you can combine them into a single dataset using: append merge Append Suppose you have two datasets with the same set (or subset) of variables to combine them vertically —> use append Open your first dataset Open your data1.dta Append the second dataset append using data2.dta Observations from data2.dta are added below those in data1.dta Merge Combine two datasets with different information that share the same key variable example —> one dataset contains GDP growth and another contains unemployment rates for the years 1990–2010 (key: year) merge 1:1 varlist using filename.dta This merges the dataset in memory with filename.dta using the key variables in varlist Merge adds columns not rows
- (^) With non-unique key Combine two datasets with different information that share the same key variable, but in one dataset the key is not unique example —> one dataset contains GDP of European regions and another contains country data merge m:1 varlist using filename.dta This merges the dataset in memory with filename.dta using the key variables in varlist - (^) After merging After the merge, Stata creates a variable called _merge It is useful to inspect this variable to check whether the data have the expected structure Check all options with: help merge Useful options include: nogen, update, replace, assert, keep Always check _merge after merging TRUE or FALSE Stata can only read .dta files. FALSE The command gen mean=1 creates a variable named mean which takes value of 1 for all observations. TRUE (^) The command drop if pop>1000 removes from the dataset all observations with population strictly larger than 1000. TRUE (^) The command merge allows you to combine two datasets that share a common key variable. TRUE
Regression —> Which straight line best summarizes the relationship between two variables It summarizes correlations It chooses the line that minimizes the sum of squared vertical distances between the points and the line Residuals vertical distances Visually: Each dot is an observation The line represents the predicted value The gap between a point and the line is the error (residual) Regression finds the line that makes these gaps as small as possible overall Regress or reg variables , robust (standard errors) Interpreting regression output Goodness of fit R-squared —> it tells us how close the dots are to the line Coefficient —> The slope of the fitted line Robust —> standard error Statistical significance P-value Correlation is not causation TRUE or FALSE: (^) The command reg runs linear regression in Stata. TRUE A regression coefficient can be interpreted as the average change in the dependent variable when the independent variable increases by one unit (holding other variables constant). TRUE Regressions in Stata allow us to compute the causal effect of one variable on another. FALSE Stata does not allow us to add control variables in regression analysis. FALSE LECTURE gen Prezzo = subinstr(Przzo, "€", "", .) replace Prezzo = trim(Prezzo) replace Prezzo = subinstr(Prezzo, ".", "", .) replace Prezzo = subinstr(Prezzo, ",", ".", .) destring Prezzo, replace m² replace metriquadri = subinstr(metriquadri, "m²", "", .) replace metriquadri = trim(metriquadri) destring metriquadri, replace keep indirizzo locali bagni piano Prezzo metriquadri twoway (scatter Prezzo metriquadri) (lfit Prezzo metriquadri), title("Apart > ment price and size") ytitle("Price") xtitle("Square meters")