When I export regression result using outreg2, how to replace se with anything I want in different columns? - stata

I want export my result generated by oaxaca command, and outreg2 gives three columns like this:
(1)
(2)
(3)
VARIABLES
overall
explained
unexplained
xxx
8.6914
-3.6527
(0.5678)
(0.5678)
xxx
8.6914
-3.6527
(0.5678)
(0.5678)
But I don't need the standard error reported in the brackets, I want to change it with a value I calculate, and the calculation method is different for columns (2) and (3).
I know I can make it by the stnum option, but how to use if to make it difference for columns (2) and (3) ?
outreg2 using east01, replace word stnum( replace se = ( abs(coef) / ex )*100 if "for columns (2)", replace se = ( abs(coef) / unex )*100 if "for columns (3)" )
BTW, ex and unex are scalar values I denoted.
Any idea?

Related

Using ARRAYFORMULA with SUMIF for multiple conditions combined with a wildcard to select all values for a given condition

I am using ARRAYFORMULA with multiple conditions using SUMIF concatenating the conditions using & and it works. Now I would like to use similarly this idea for a special condition indicating to consider all values using a wildcard ("ALL") for a given column, but it doesn't work.
Here is my example:
On I2if have the following formula:
=ARRAYFORMULA(if(not(ISBLANK(H2:H)),sumif(B2:B & C2:C & D2:D & year(E2:E),
if($G$2="ALL",B2:B,$G$2) & if($G$4="ALL",C2:C,$G$4) & if($G$6="ALL",D2:D,$G$6) &
H2:H,A2:A),))
and it works, when I enter specific values, but when I use my wildcard: ALL indicating that for a given column/criteria all values should be taken into consideration, it doesn't work as expected. The scenario should consider that all criteria can be labeled as ALLin such case it will provide the sum of NUM per year.
Here is my testing sample in Google Spreadsheet:
https://docs.google.com/spreadsheets/d/1c28KRQWgPCEdzVvwvXFOQ3Y13MBDjpEgKdfoLipFAOk/edit?usp=sharing
Notes:
I was able to get a solution for that using SUMPRODUCT but this function doesn't get expanded with ARRAYFORMULA
In my real example I have more conditions, so I am looking for a solution that escalates having more conditions
use:
=QUERY(QUERY(FILTER(A2:E,
IF(G2="All", B2:B<>"×", B2:B=G2),
IF(G4="All", C2:C<>"×", C2:C=G4),
IF(G6="All", D2:D<>"×", D2:D=G6)),
"select year(Col5),sum(Col1)
where Col1 is not null
group by year(Col5)"),
"offset 1", 0)

Using query to select multiple headers instead of columns

I'm able to dynamically select a 1-dimensional array using the header name instead of a column letter.
=query(A:J, "select "&substitute(address(1,match("Header2",A1:J1,0,4),"1","")&"")
Is it possible to do this with a 2-dimensional array? So instead of selecting B:C, I would select Header2 : Header3?
I've tried using a couple variations of concat operator without success.
=query(A:J, "select "&substitute(address(1,match("Header2",A1:J1,0,4),"1","")&" "&:&" "&substitute(address(1,match("Header3",A1:J1,0,4),"1","")&"")
If it is useful or presents a possible workaround, the columns in question will always stay adjacent to each other. Header1 next to Header2 next to Header3, etc. However, Header1 could be in any column.
transpose, select headers, transpose back:
=TRANSPOSE(QUERY(TRANSPOSE(A:J), "where Col1 matches 'Header2|Header3|Header4'"))
or:
=FILTER(A:J, REGEXMATCH(A1:J1, "Header2|Header3|Header4"))
or:
=INDEX(INDIRECT(ADDRESS(1, MATCH("Header2", 1:1, ))&":"&
ADDRESS(ROWS(A:A), MATCH("Header4", 1:1, ))))
You seem to have a missing ) at the end of the match.
Once fixed, you can expand your solution to add other headers like this:
=query(A:J, "select "
&substitute(address(1,match("Header2",A1:J1,0),4),"1","")&","
&substitute(address(1,match("Header3",A1:J1,0),4),"1","")&","
&substitute(address(1,match("Header4",A1:J1,0),4),"1","")&"
")

PowerQuery: How to replace text with each column name for multiple columns

I'm trying to replace "x" in each column (excepts for the first 2 columns) with the column name in a table with an unknown number of columns but with at least 2 columns.
I found the code to change one column, but I want it to be dynamic:
#"Ersatt värde" = Table.ReplaceValue(Källa,"x", Table.ColumnNames(Källa){2},Replacer.ReplaceText,{Table.ColumnNames(Källa){2}})
Any ideas on how to solve it?
If I understand correctly, I think you can try either approach below:
#"Ersatt värde" =
let
columnsToTransform = List.Skip(Table.ColumnNames(Källa), 2),
accumulated = List.Accumulate(columnsToTransform, Källa, (tableState as table, columnName as text) =>
Table.ReplaceValue(tableState,"x", columnName, Replacer.ReplaceText, {columnName})
)
in accumulated
or:
#"Ersatt värde" =
let
columnsToTransform = List.Skip(Table.ColumnNames(Källa), 2),
transformations = List.Transform(columnsToTransform, (columnName) => {columnName, each
Replacer.ReplaceText(Text.From(_), "x", columnName)}),
transformed = Table.TransformColumns(Källa, transformations)
in transformed,
Both ways follow a similar approach:
Figure out which columns to do replacements in (i.e. all except the first 2 columns)
Loop over columns determined in previous step and actually do the replacement.
I've used Replacer.ReplaceText since that's what you'd used in your question, but I believe this will replace both partial matches and full matches.
If you only want full matches to be replaced, I think you can use Replacer.ReplaceValue instead.

Number of groups in esttab (after xtmixed)

i'd like to add the number of groups to a esttab output after fitting a multilevel model in Stata (xtmixed). I found this post and understand that the number of groups is stores in the matrix e(N_g). However, I do not understand, how to use the value in this matrix and add it to the output. I tried estadd but did not manage to get it work. I also did not find something about using a single cell of a matrix in an output in the estout manual.
Any ideas on how to add the number of gorups to a esttab output? Or maybe experiences with adding single values from a matrix?
Thx!!!
P.S. This is the output of matrix list e(N_g)
symmetric e(N_g)[1,1]
c1
r1 117
Estadd is the way to go, here is an example:
clear all
webuse pig
// model
eststo: xtmixed weight week || id:, vce(robust)
// estadd portion - pull the single element of the matrix into a local and estadd it
matrix N_g = e(N_g)
local groups = N_g[1,1]
estadd local groups `groups'
// add groups in the stats option and it will appear at the bottom along
// with the number of observations in this case
esttab ., stats(N groups)
Actually, esttab, stats(N_g, labels("N")) is sufficient. estout can recognize N_g.

SQL and regular expression to check if string is a substring of larger string?

I have a database filled with some codes like
EE789323
990
78000
These numbers are ALWAYS endings of a larger code. Now I have a function that needs to check if the larger code contains the subcode.
So if I have codes 90 and 990 and my full code is EX888990, it should match both of them.
However I need to do it in the following way:
SELECT * FROM tableWithRecordsWithSubcode
WHERE subcode MATCHES [reg exp with full code];
Is a regular expression like this this even possible?
EDIT:
To clarify the issue I'm having, I'm not using SQL here. I just used that to give an example of the type of query I'm using.
In fact I'm using iOS with CoreData, and I need a predicate to fetch me only the records that match.
In the way that is mentioned below.
Given the observations from a comment:
Do you have two tables, one called tableWithRecordsWithSubcode and another that might be tableWithFullCodeColumn? So the matching condition is in part a join - you need to know which subcodes match any of the full codes in the second table? But you're only interested in the information in the tableWithRecordsWithSubcode table, not in which rows it matches in the other table?
and the laconic "you're correct" response, then we have to rewrite the query somewhat.
SELECT DISTINCT S.*
FROM tableWithRecordsWithSubcode AS S
JOIN tableWithFullCodeColumn AS F
ON F.Fullcode ...ends-with... S.Subcode
or maybe using an EXISTS sub-query:
SELECT S.*
FROM tableWithRecordsWithSubcode AS S
WHERE EXISTS(SELECT * FROM tableWithFullCodeColumn AS F
WHERE F.Fullcode ...ends-with... S.Subcode)
This uses a correlated sub-query but avoids the DISTINCT operation; it may mean the optimizer can work more efficiently.
That just leaves the magical 'X ...ends-with... T' operator to be defined. One possible way to do that is with LENGTH and SUBSTR. However, SUBSTR does not behave the same way in all DBMS, so you may have to tinker with this (possibly adding a third argument, LENGTH(s.subcode)):
LENGTH(f.fullcode) >= LENGTH(s.subcode) AND
SUBSTR(f.fullcode, LENGTH(f.fullcode) - LENGTH(s.subcode)) = s.subcode
This leads to two possible formulations:
SELECT DISTINCT S.*
FROM tableWithRecordsWithSubcode AS S
JOIN tableWithFullCodeColumn AS F
ON LENGTH(F.Fullcode) >= LENGTH(S.Subcode)
AND SUBSTR(F.Fullcode, LENGTH(F.Fullcode) - LENGTH(S.Subcode)) = S.Subcode;
and
SELECT S.*
FROM tableWithRecordsWithSubcode AS S
WHERE EXISTS(
SELECT * FROM tableWithFullCodeColumn AS F
WHERE LENGTH(F.Fullcode) >= LENGTH(S.Subcode)
AND SUBSTR(F.Fullcode, LENGTH(F.Fullcode) - LENGTH(S.Subcode)) = S.Subcode);
This is not going to be a fast operation; joins on computed results such as required by this query seldom are.
I'm not sure why you think that you need a regular expression... Just use the charindex function:
select something
from table
where charindex(code, subcode) <> 0
Edit:
To find strings at the end, you can create a pattern with the % wildcard from the subcode:
select something
from table
where '%' + subcode like code