I have a probit model and I'm trying to compute and plot the marginal effects of a continuous variable for all the observations in the sample.
I'm using Stata and I have five independent variables. The variable for which I would like to compute marginal effects for all individuals takes 9 possible integer values (0 to 8), and I treat it as continuous (not as a factor variable). The most common command for marginal effects seems to be margins:
margins, dydx(x) at(x=(0(1)8))
marginsplot
This command doesn't do what I would like to do. It seems to compute marginal effects for all individuals with x=0, x=1 and so forth and then average them for each value of x. The output I get reports a marginal effect and a standard error for each value of x.
I would like to obtain the marginal effect of x for each individual in the sample. Two individuals for whom x=0 should have different marginal effects if the other variables in the model take different values for these observations. How can compute and plot these effects?
You can estimate the model, predict, change x with replace, and predict again. Then you can calculate the differences between the two predictions to get the marginal effects. You can change x by 1 or by a small amount epsilon (which will approximate what margins calculates much better). The first version will obviously be larger.
Here's an example using the cars data:
set more off
sysuse auto, clear
keep foreign mpg rep78 make
clonevar orig_rep78 = rep78
probit foreign mpg rep78
margins, dydx(rep78)
predict phat, pr
replace rep78 = rep78 + 1
predict phat_plus_one, pr
gen double finite_diff = phat_plus_one - phat
replace rep78 = orig_rep78 + 0.01
predict phat_plus_eps, pr
gen double eps_diff = (phat_plus_eps - phat)/.01
drop rep78
rename orig_rep78 rep78
tw (scatter finite_diff mpg) (scatter eps_diff mpg)
sum *_diff
You can see that the eps version matches the margins output more closely. You might want to see how sensitive your estimates are to different values of epsilon.
I don't know what sort of graph you had in mind, so I just plotted the MEs against the other regressor. You can see that the ME is increasing in mpg, which makes sense since the index function coefficients are all positive. The low values for mpg above 30 make sense since the predicted probability is already near 1, so additional repairs can't raise it all that much.
Related
I am new to generalised linear modelling. I ran the negative binomial model, and then try to estimate the residuals from the model.
Here is what I did:
Run a negative binomial regression model with nbreg command in stata 17.
Run the predict command to estimate the predicted values.
Then, generate the residual by subtracting predicted values from observed values.
Did I do it correctly?
I am working on a question that asks me to solve for the weighted average of my dependent variable (hourly wage) by using the weight of my independent variable (which is a discrete variable that has 16 categories and more than 300,000 observations). as you can see below.
enter image description here
how am I suppose to generate the weighted variable for a variable that has so many observations?
First you should determine whether the weights of x are sampling weights, frequency weights or analytic weights. Then, if y is your dependent variable and x_weights is the variable that contains the weights for your independent variable, type in:
mean y [pweight = x_weight] for sampling (probability) weights
mean y [fweight = x_weight] for frequency weights
mean y [aweight = x_weight] for analytic weights
You can find a nice summary of these different options here, as well as information on the more specialized option iweight.
I want to plot the average weight (y-axis) by make (x-axis) and sort it so the heaviest make is the leftmost on the x-axis and the lightest is the rightmost on the x-axis. I thought the sort option would work.
sysuse auto, clear
keep if foreign
sort mpg
gen obsno = _n
scatter weight obsno, xla(1/22) sort(weight)
The sort() option is allowed with scatter because one of the possibilities of scatter is to connect points with a line. But it refers only to the order in which points are connected. The default is to connect points in the current sort order of the dataset. In practice the most common example is that observations are in some time order or follow some other sequence but even then scatter would respect the order of a time or other variable only if the data were in the same order -- unless, to complete the circle, a sort order were specified with this option.
You are not asking for any connnection. In that circumstance, sort() remains legal but it is ignored as of no relevance to what you're asking for.
There is no circumstance in which sort() has another effect with scatter and it will not change either axis variable to something else.
A way to get what I think you want is with the undocumented vertical option of graph dot. Some of the small choices here are just my personal idea of what looks good. For example, I have found that the default dotted grid lines often copy poorly to other software, so I use thin light grey continuous lines as a grid.
sysuse auto, clear
keep if foreign
sort mpg
gen obsno = _n
graph dot (asis) weight, over(obsno, sort(1) descending) vertical ///
linetype(line) lines(lcolor(gs12) lw(vthin)) yla(, ang(h))
It's perfectly possible to get a similar graph using scatter, just more work as you have to arrange that the observation numbers become the value labels of a variable defining the sort order.
See also quantile and qplot (Stata Journal).
I'm using PROC LOGISTIC procedure in SAS and option SELECTION=SCORE which gives me few logistic regression models and their Chi-Square values. My question would be which model is better - with smaller Chi-Square or bigger?
In general, the larger chi-squared statistic will correspond with a lower p-value (more significance). However, it is important to know the shape of the chi-squared distribution and also the number of degrees of freedom. As you can see in the graph, the relationship between p and chi-squared changes based on the degrees of freedom.
Score for Chi-Square is larger, the model is better.
For example, the graph for the weighted scatter plot on the official Stata website is:
webuse census
scatter death medage [w=pop65p], msymbol(circle_hollow)
If I use the mlabel option, my points are labelled but are no longer weighted. Is there a way to accomplish both at the same time?
Thanks!
You can overlay two plots:
clear all
set more off
sysuse auto
gen somew = _n
twoway scatter price mpg [w=somew], msymbol(circle_hollow) || ///
scatter price mpg, msymbol(i) mlabel(make) ||, legend(off)
The solution is from Statalist. Nick Cox has some "awkward questions" related to the use of such plots, you might want to read.