Mymean2.ado does not save the results that it displays. display "The variance of the estimator is " (_se)^2 The estimated variance of the average is. I now illustrate that it works for another variable. The estimated variance of the average is 117561.16
#Stata summation how to#
I begin by illustrating how to replicate the previous results. I will discuss syntax in greater detail in subsequent posts. The extremely powerful syntax command puts the elements of Stata syntax specified by the user into local macros and throws errors when the user makes a mistake. Generate double e2 = (`varlist' - `mu')^2ĭisplay "The average of `varlist' is " `mu'ĭisplay "The estimated variance of the average is " `V' On line 5 of mymean2.ado, we use the syntax command to store the name of the variable specified by the user into the local macro varlist, which we use in the remainder of the computations.ĭisplay "The local macro varlist contains `varlist'" We want our command to be reusable we want it to estimate the mean for any variable in memory, instead of only for price as performed by mymean1.ado. The mymean1 command performs the same computations as the do-file mean1.do. The command name must be the same as the file name that precedes the suffix. Line 1 of mymean1.ado specifies that file defines the command mymean1. (The file mymean1.ado is in my current working directory.)
#Stata summation code#
The code in mymean1.ado performs the same calculations as mean1.do. Now I verify that mean produces the same results. \widehat\).)Įxample 1: Computing the average and its sampling variance The formulas for the sample average and its estimated sampling variance, assuming an independently and identically distributed process, are See Programming an estimation command in Stata: A map to posted entries for a map to all the posts in this series. I recommend that you start at the beginning.
#Stata summation series#
This is the fourth post in the series Programming an estimation command in Stata. Along the way, I illustrate some of the postestimation features that work after the command. I subsequently introduce ado-file programming and discuss two versions of the command. I begin by reviewing the formulas and a do-file that implements them. The command that I discuss estimates the mean by the sample average. More detail than you probably want to know about this technique is here, but the short version is that line creates a new variable, unique, that equals 1 for the first observation of a unique value of mpg and 0 else.I discuss the code for a simple estimation command to focus on the details of how to implement an estimation command. I'll use the auto.dta data for the sake of example, and I'll calculate the sum of unique values of mpg, a bit of a nonsense measure but it demonstrates the point well enough: sysuse autoĪ smarter and non-destructive way would use by to flag unique values, then use that flag to conditionally sum the values of mpg: sysuse auto Other users who have found the question cross-posted are encouraged to share the links as a reply as well.Īs /u/makemeking706 suggests, you have two problems here: 1) what's a unique value? and 2) what's the sum of those values.Ī destructive and dumb-but-works way is to create a frequency distribution and use its result to sum the unique values. If you've asked a question, let people know where else you asked the question and what your solution(s) were! When you post a question on another platform, include those links in your questions or as a reply (if it's Discord, just mention it).See the sticked "READ ME: How to best ask for help in /r/Stata" post on how to comment here if all else fails. Make sure to include the word "Stata" in your search query. Perform a web search for your question prior to posting here.
Stata's online community has been active for many years and many questions and solutions are documented on StataList, which are highly indexed on contemporary search engines (e.g., Google). Stata has extensive and complete documentation you can read before posting here (and you can type help followed by the command name in console to see it, e.g. This is not a place to find Stata tutoring. Do not request that the /r/Stata community do your homework for you. Assume good faith questions and comments.