Stata does not appear to accept forward slashes in Stata 16 - stata

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.

Related

SEM: Cross-lagged model on 5 waves with fixed effects

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)

Stata coefplot: plot coefficients and corresponding confidence intervals on 2nd axis

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?

How to plot PCA with paired data?

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)

survival analysis in shiny

I met a problem with presenting my survival analysis by shiny app. I upload a dataset for my analysis and use 3 selectInputs to determine the time censor and group. However when I want to use for example input$si5ax as my time variable, it comes a error that Time variable is not numeric.
the code is as follows:
data_set <- reactive({
infile <- input$dataset3
if (is.null(infile)) {
return(NULL)
}
infile3 <- input$dataset3
read.csv(infile3$datapath,sep=',')
})
aea8 <- eventReactive(input$doa8,{
fit1 <- survfit(Surv(input$si5ax, censor)~group, data = data_set())
ggsurv(fit1)+theme_bw(base_size = 16)
})
output$plota1 <- renderPlot({
aea8()
})
This problem will be solved when I use time directly instead of input selection. The same problem takes place by variable censor and group. The error message said the length of group is not equal.
I am looking forward for helping

Pattern matching in dataset

been struggling with this for a while.
I have a dataset with two columns, a Description column and the other is the pattern column that I am trying to match against the description column.If the corresponding pattern exists in the Description column, it needs to be replaced by an asterisk
For instance, if the Description is ABCDEisthedescription and the Pattern is ABCDE, then the new description should *isthedescription
I tried the following
data$NewDescription <- gsub(data$pattern,"\\*",Data$Description )
since there is more than one row in the dataset, it throws an error ( a warning rather)
"argument 'pattern' has length > 1 and only the first element will be used"
Any help will be hugely appreciated.
You can use an mapply here to apply the function to each row.
#sample data
data<-data.frame(
pattern=c("ABCDE","XYZ"),
Description=c("ABCDEisthedescription", "sillyXYZvalue")
)
Now use mapply
mapply(function(p,d) gsub(p, "\\*", d, fixed=T), data$pattern, data$Description)
# [1] "\\*isthedescription" "silly\\*value"
Additionally,
Patterns <- paste0(
sample(LETTERS[1:4],500,replace=TRUE),
sample(LETTERS[1:4],500,replace=TRUE),
sample(LETTERS[1:4],500,replace=TRUE),
sample(LETTERS[1:4],500,replace=TRUE))
##
Desc <- paste0(Patterns,"isthedescription")
Ptrn <- sample(Patterns,500)
##
Data <- data.frame(
Description=Desc,
Pattern=Ptrn,
stringsAsFactors=FALSE)
##
newDesc <- sapply(1:nrow(Data), function(X){
if(substr(Data$Description[X],1,4)==Data$Pattern[X]){
gsub(Data$Pattern[X],"*",Data$Description[X])
} else {
Data$Description[X]
}
})
#MrFlick's approach seems more concise though.