I have currently run a cross-lagged model on 5 waves to answer the question does depression affect self-esteem, or does self-esteem affect depression, using the following code:
sem (depression2 <- depression1 inattention1) ///
(depression <- depression2 selfesteem2) ///
(depression4 <- depression3 selfesteem3) ///
(depression5 <- depression4 selfesteem4) ///
(selfesteemn2 <- selfesteem1 depression1) ///
(selfesteem3 <- selfesteem2 depression2) ///
(selfesteem4 <- selfesteem3 depression3) ///
(selfesteem5 <- selfesteem4 depression4), ///
method(mlmv) standardized ///
cov(e.depression2*e.selfesteem2 e.depression3*e.selfesteem3 e.depression4*e.selfesteem4 e.depression5*e.selfesteem)
This all went well; however, I would like to include fixed effect of gender and IQ and I have absolutely no clue how to do it. I found some slides by Paul Allison that were cut off at the point where he was supposed to explain how to do this. Can anyone advise me how to achieve this?
You will need to include a new regression before the comma with the fixed effects. Something like this could work:
(gender IQ -> depression depression2 depression3 depression4 depression5) ///
(gender IQ -> selfesteemn2 selfesteem3 selfesteem4 selfesteem5) ///
Keep in mind that sem doesn't accept the i. notation natively, so you will have to generate indicator variables for categorical variables using xi: like this:
xi: sem (IQ <- i.subgroup)
Related
Example from here.
webuse sem_sm2, clear
sem ///
(anomia67 pwless67 <- Alien67) /// measurement piece
(anomia71 pwless71 <- Alien71) /// measurement piece
(Alien67 <- SES) /// structural piece
(Alien71 <- Alien67 SES) /// structural piece
(SES -> educ occstat66), nolog // measurement piece
What I get is somehow:
. sem ///
invalid use of '/'
r(198);
.
. (anomia67 pwless67 <- Alien67) /// measurement piece
( is not a valid command name
r(199);
.
. (anomia71 pwless71 <- Alien71) /// measurement piece
( is not a valid command name
r(199);
.
. (Alien67 <- SES) /// structural piece
( is not a valid command name
r(199);
.
. (Alien71 <- Alien67 SES) /// structural piece
( is not a valid command name
r(199);
.
. (SES -> educ occstat66), nolog // measurement piece
( is not a valid command name
r(199);
Could anyone tell me what is going on here? A colleague can run this in a different version. Did the syntax change?
See [U] 16.1.2 in the manuals for basic explanation of comments.
The /* /, //, and /// comment indicators can be used in do-files and
ado-files only; you may not use them interactively. You can, however,
use the ‘’ comment indicator interactively.
When trying to depict two coefficients from one regression on separate axes with Ben Jann's superb coefplot (ssc install coefplot) command, the coefficient to be shown on the 2nd axis is correctly displayed, but its confidence interval is depicted on the 1st scale.
Can anyone explain how I get the CI displayed on the same (2nd) axis as the coefficient it belongs to? I couldn't find any option to change this - and imagine it should be the default, if not the only, option to plot the CI around the point estimate it belongs to.
I use the latest coefplot version with Stata 16.
Here is a minimum example to illustrate the problem:
results plot
webuse union, clear
eststo results: reg idcode i.union grade
coefplot (results, keep(1.union)) (results, keep(grade) xaxis(2))
In the line
coefplot (results, keep(1.union)) (results, keep(grade) xaxis(2))
you specify the option xaxis(2), but this is not a documented option of coefplot, although it is a valid option of twoway rspike which is called by coefplot. Apparently, if you use xaxis(2) something goes wrong with the communication between coefplot and rspike.
This works for me:
coefplot (results, keep(1.union)) (results, keep(grade) axis(2))
I'm trying to create something similar. Since this option is not built-in we need to write a program to tweak how coefplot works. I'm sharing the code from the user manual here: http://repec.sowi.unibe.ch/stata/coefplot/markers.html
capt program drop coefplot_mlbl
*! version 1.0.0 10jun2021 Ben Jann
program coefplot_mlbl, sclass
_parse comma plots 0 : 0
syntax [, MLabel(passthru) * ]
if `"`mlabel'"'=="" local mlabel mlabel(string(#b, "%5.2f") + " (" + string(#ll, "%5.2f") + "; " + string(#ul, "%5.2f") + ")")
preserve
qui coefplot `plots', `options' `mlabel' generate replace nodraw
sreturn clear
tempvar touse
qui gen byte `touse' = __at<.
mata: st_global("s(mlbl)", ///
invtokens((strofreal(st_data(.,"__at","`touse'")) :+ " " :+ ///
"`" :+ `"""' :+ st_sdata(.,"__mlbl","`touse'") :+ `"""' :+ "'")'))
sreturn local plots `"`plots'"'
sreturn local options `"`options'"'
end
capt program drop coefplot_ymlbl
*! version 1.0.0 10jun2021 Ben Jann
program coefplot_ymlbl
_parse comma plots 0 : 0
syntax [, MLabel(str asis) * ]
_parse comma mlspec mlopts : mlabel
local mlopts = substr(`"`mlopts'"', 2, .) // remove leading comma
if `"`mlspec'"'!="" local mlabel mlabel(`mlspec')
else local mlabel
coefplot_mlbl `plots', `options' `mlabel'
coefplot `plots', ///
yaxis(1 2) yscale(alt) yscale(axis(2) alt noline) ///
ylabel(none, axis(2)) yti("", axis(2)) ///
ymlabel(`s(mlbl)', axis(2) notick angle(0) `mlopts') `options'
end
coefplot_ymlbl D F, drop(_cons) xline(0)
However, the above program does not allow for the option 'bylabel'. I get a stata error saying "bylabel not allowed". I wanted to ask if there is a way to edit this code and include the bylabel option which is used to label subplots?
I am currently working with genetic data from different patients. To date I have always worked with PCAs by comparing independent groups. Example: (Sick Vs Control, Treatment Vs Control etc.)
But now I have paired data. I mean that there exists a relationship between the samples of different groups. The typical example is having a group of subjects and measuring each of them before and after treatment.
I did this PCA with Thermofisher program, but I would like to do in R. This is the output of the ThermoFisher program. B (Before treatment) P (Post-treatment)
I tried to looking for any example in Google, but I didn't found it.
An example would be something like this:
data.matrix <- matrix(nrow=100, ncol=10)
colnames(data.matrix) <- c(
paste("P_BT", 1:5, sep=""),
paste("P_AT", 1:5, sep=""))
rownames(data.matrix) <- paste("gene", 1:100, sep="")
for (i in 1:100) {
wt.values <- rpois(5, lambda=sample(x=10:1000, size=1))
ko.values <- rpois(5, lambda=sample(x=10:1000, size=1))
data.matrix[i,] <- c(wt.values, ko.values)
}
head(data.matrix)
busan<-subset(influ_busan, select = c(CNT,temp_min,temp_diff,humid_mean,hpa_mean,rad_mean,wind_mean,o3))
new_busan<-mice(busan, seed=12345, n=5)
lm_busan <- with(new_busan,lm(CNT~temp_min+temp_diff+humid_mean+hpa_mean+rad_mean+wind_mean+o3))
summary(lm_busan)
busan_predict<-data.frame(fitted.values(lm_busan))
This is my simply version syntax. I use multiple imputation for NA and After multiple imputation, I want to extract fitted values. However I can't extract fitted values, how can I extract fitted values?
You can do this via extract_imputations function from my version of mice; hopefully will be incorporated into the main mice version shortly:
see: https://github.com/stefvanbuuren/mice/pull/51
devtools::install_github("alexwhitworth/mice")
library(mice)
new_busan <- mice(busan, seed= 12345, m=2)
busan_predict <- extract_imputations(busan, new_busan$imp, j= 1)
busan_predict <- extract_imputations(busan, new_busan$imp, j= 2)
Edit Apparently, I didn't read the mice documentation thoroughly enough. This functionality already existed in mice -- mice::complete
I have the following model:
Y_{it} = alpha_i + B1*weight_{it} + B2*Dummy_Foreign_{i} + B3*(weight*Dummy_Foreign)_ {it} + e_{it}
and I am interested on the effect on Y of weight for foreign cars and to graph the evolution of the relevant coefficient across quantiles, with the respective standard errors. That is, I need to see the evolution of the coefficients (B1+ B3). I know this is a non-linear effect, and would require some sort of delta method to obtain the variance-covariance matrix to obtain the standard error of (B1+B3).
Before I delve into writing a program that attempts to do this, I thought I would try and ask if there is a way of doing it with grqreg. If this is not possible with grqreg, would someone please guide me into how they would start writing a code that computes the proper standard errors, and graphs the quantile coefficient.
For a cross section example of what I am trying to do, please see code below.
I use grqred to generate the evolution of the separate coefficients (but I need the joint one)-- One graph for the evolution of (B1+B3) with it's respective standard errors.
Thanks.
(I am using Stata 14.1 on Windows 10):
clear
sysuse auto
set scheme s1color
gen gptm = 1000/mpg
label var gptm "gallons / 1000 miles"
gen weight_foreign= weight*foreign
label var weight_foreign "Interaction weight and foreign car"
qreg gptm weight foreign weight_foreign , q(.5)
grqreg weight weight_foreign , ci ols olsci reps(40)
*** Question 1: How to constuct the plot of the coefficient of interest?
Your second question is off-topic here since it is statistical. Try the CV SE site or Statalist.
Here's how you might do (1) in a cross section, using margins and marginsplot:
clear
set more off
sysuse auto
set scheme s1color
gen gptm = 1000/mpg
label var gptm "gallons / 1000 miles"
sqreg gptm c.weight##i.foreign, q(10 25 50 75 95) reps(500) coefl
margins, dydx(weight) predict(outcome(q10)) predict(outcome(q25)) predict(outcome(q50)) predict(outcome(q75)) predict(outcome(q95)) at(foreign=(0 1))
marginsplot, xdimension(_predict) xtitle("Quantile") ///
legend(label(1 "Domestic") label(2 "Foreign")) ///
xlabel(none) xlabel(1 "Q10" 2 "Q25" 3 "Q50" 4 "Q75" 5 "Q95", add) ///
title("Marginal Effect of Weight By Origin") ///
ytitle("GPTM")
This produces a graph like this:
I didn't recast the CI here since it would look cluttered, but that would make it look more like your graph. Just add recastci(rarea) to the options.
Unfortunately, none of the panel quantile regression commands play nice with factor variables and margins. But we can hack something together. First, you can calculate the sums of coefficients with nlcom (instead of more natural lincom, which the lacks the post option), store them, and use Ben Jann's coefplot to graph them. Here's a toy example to give you the main idea where we will look at the effect of tenure for union members:
set more off
estimates clear
webuse nlswork, clear
gen tXu = tenure*union
local quantiles 1 5 10 25 50 75 90 95 99 // K quantiles that you care about
local models "" // names of K quantile models for coefplot to graph
local xlabel "" // for x-axis labels
local j=1 // counter for quantiles
foreach q of numlist `quantiles' {
qregpd ln_wage tenure union tXu, id(idcode) fix(year) quantile(`q')
nlcom (me_tu:_b[tenure]+_b[tXu]), post
estimates store me_tu`q'
local models `"`models' me_tu`q' || "'
local xlabel `"`xlabel' `j++' "Q{sub:`q'}""'
}
di "`models'
di `"`xlabel'"'
coefplot `models' ///
, vertical bycoefs rescale(100) ///
xlab(none) xlabel(`xlabel', add) ///
title("Marginal Effect of Tenure for Union Members On Each Conditional Quantile Q{sub:{&tau}}", size(medsmall)) ///
ytitle("Wage Change in Percent" "") yline(0) ciopts(recast(rcap))
This makes a dromedary curve, which suggests that the effect of tenure is larger in the middle of the wage distribution than at the tails: