I have to analyze a regression and estimate how the crime rate responds to changes in the state unemployment rate (unemp in the Stata output). The relationship between the two variables is not linear, and if a linear model is fitted anyway, the errors do not have the distributional properties that a regression. If you're new to Stata we highly recommend starting from the beginning.I n the beer sales example, a simple regression fitted to the original variables (price-per-case and cases-sold for 18-packs) yields poor results because it makes wrong assumptions about the nature of the patterns in the data. We simply transform the dependent variable and fit linear regression models like this:This is part six of Introduction to Stata. Fortunately, there is a text option to create logs in plain text format, which can be viewed in an editor such as Notepad or a word processor such as Word.The natural log transformation is often used to model nonnegative, skewed dependent variables such as wages or cholesterol. By default the log is written using SMCL, Stata Markup and Control Language (pronounced smickle), which provides some formatting facilities but can only be viewed using Stata’s Viewer.Display log(10) will display the answer 2.3026. Display 2+2 will display the answer 4, while. Generate and Replace2.5 Using Stata as a calculator The display command can be used to carry out simple calculations. This article will teach you the basics of making new variables, modifying existing variables, and creating labels.For example, to take the natural log of v1 and create a new variable (for example, v1log).The primary commands for creating and changing variables are generate (usually abbreviated gen) and replace (which, like other commands that can destroy information, has no abbreviation). Display log10(1000)A VAR with plags is usually Title stata. To obtain base 10 logarithms use the log10 function.
Natural Log Stata How To Stretch ThisYou might be tempted to try to add code that "fixes" the price2020 variable (say, multiplying it by 4.14/4). You'll learn how to stretch this one-observation-at-a-time paradigm in Data Wrangling in Stata, but tasks that break it (like calculating means) require a different approach that we'll talk about soon.Suppose we wanted to be a little more precise and use 4.14 as the conversion factor. This is a good example of how to check your work: compare what you got to what you expected, and if they don't match make sure you know why!Internally, Stata executed a loop: it calculated price*4 for the first observation and stored the result in price2020 for the first observation, then calculated price*4 for the second observation and stored the result in price2020 for the second observation, and so forth for all the observations in the data set. Once you start including if conditions, how many observations were actually changed can be very useful information.Exercise: Outside the United States, fuel efficiency is frequently measured in liters per kilometer (note that because the fuel used is in the numerator, a low number is good). Given that the data set has 74 observations this tells you that all of them were changed, as you'd expect. But if you only want to work with 2020 dollars and are confident you've got the formula right, you can use the replace command to change the existing price variable instead of creating a new one:Run this version and you'll get the message (74 real changes made). Because your do file loads the original data from disk every time it is run, it can simply create the price2020 variable the way it should be.Having both price and price2020 allowed you to compare their values and check your work. Change:And run the do file again. However it will be assigned a missing value for observations where the if condition is not true. Creating Variables with If ConditionsIf a gen command has an if condition, the resulting variable will (and must) still exist for all observations. Create a variable that stores the fuel efficiency of each car in liters per kilometer. It is designed solely for recoding tasks and is much less flexible than gen and replace. RecodeThe recode command gives you an alternative way of creating rep3. Rep4 should go from one to four). Create a rep4 variable that combines the ones and twos and renumbers the other categories accordingly (i.e. Remember, missing is essentially infinity.)Exercise: Combining the ones and twos makes sense because there are so few of them, but there was no particular need to combine the fours and fives. Anything not covered by a rule is left unchanged, so you can use recode to change just a few values of a variable or completely redefine it as we do here. The outputValue will always be a single number. The inputValue can be a single number, a list of numbers separated by spaces, or a range of numbers specified with start/ end. They take the form ( inputValue = outputValue ). You can also have recode work on a list of variables, recoding them all in the same way.The core of the recode command is a list of rules, each in parentheses, that tell it how a variable is to be recoded. The syntax is:Recode var ( rule 1) ( rule 2) ( more rules as needed.), gen( newvar)The gen option at the end is not required—if it's not there then the original variable will be changed rather than creating a new variable with the new values. Spiderwick chronicles pc game cheatsHow would your code change if the indicator variable needed to identify cars that are known to have good repair records? String VariablesThe gen and replace commands work with string variables too. The lowMPG variable should be missing for such cases, which you can do with:Exercise: Create an indicator variable that identifies cars with good repair records (defined as rep78 greater than 3). To see the results run the do file and then type browse make mpg if lowMPG.No car has a missing value for mpg, but if one did, the above code would assign it a zero for lowMPG as if it were known to have good gas mileage. Consider:(The parentheses are optional, but make it easier to read.) This creates an indicator variable called lowMPG which is one (true) for cars where mpg is less than twenty and zero (false) where mpg is greater than or equal to twenty. Indicator VariablesIn creating indicator variables you can take advantage of the fact that Stata treats true as one and false as zero by setting the new variable equal to a condition. The second is a number telling it which word you want. The first input, or argument, for the word() function is the string to act on (in this case a variable containing strings). You can easily extract the company name using the word() function:To see the results, run the do file and type browse make company. But most work with strings is done by special-purpose functions that take strings as input (either string values or variables containing strings) and return strings as output.The make variable really records two pieces of information: the name of the company that produced the car, and the name of the car model. Stata would not find this confusing (though you might) because x in quotes ( "x") means the letter x and x without quotes means the variable x.Addition for strings is defined as putting one string after the other, so "abc" + "def" = "abcdef". String values always go in quotes, so if you wanted to store the letter x in a variable called x you'd say gen x = "x".
0 Comments
Leave a Reply. |
AuthorKarla ArchivesCategories |