Understanding the use of local command in Randomization Check - loops

I am trying to run a do-file for an RCT. The authors are using the local command.
They create a local macro taking the value 1. Based on that, they are creating missing values for the var variable. However, after that, I cannot understand why they are adding 1 to it. Moreover, why are the numvar and numvarse being used for mean and sd?
Any explanation would be really helpful.
The code:
*RANDOMIZATION CHECK
local numvar=1
foreach var in T1_Client_Age T1_Client_Married T1_HH_Size T1_Client_Literate T1_Client_Education T1_muslim ///
T1_Hindu_SC_Kat T1_rest3 T1_Log_HH_Income T1_Household_Business ///
T1_Client_Wage_Salaried_2 T1_Client_Bus_PR_Employed_2 T1_Client_Housewife_2 {
replace `var'=. if miss_`var'==1
local numvarse = `numvar' + 1
replace varname="`var'" in `numvar'
*MEAN AND SD FOR CONTROL
quietly: sum `var' if Treated_All==0
replace Control=r(mean) in `numvar'
replace Control=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED
quietly:sum `var' if Treated_All==1
replace Treat=r(mean) in `numvar'
replace Treat=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED WITH FRIEND
quietly:sum `var' if Treatment_Peer==1
replace Treat_Peer=r(mean) in `numvar'
replace Treat_Peer=r(sd) in `numvarse'
*MEAN DIFFERENCES BETWEEN TREATED AND CONTROL
xi:reg `var' Treated_All i.sewa_center*i.baseline i.t_month , cluster(t_group)
replace Diff_Control_Treat=_b[Treated_All] in `numvar'
replace Diff_Control_Treat=_se[Treated_All] in `numvarse'
*MEAN DIFFERENCES BETWEEN TREATED ALONE AND WITH FRIEND
xi:reg `var' Treated_All Treatment_Peer i.sewa_center*i.baseline i.t_month , cluster(t_group)
replace Diff_Alone_Peer=_b[Treatment_Peer] in `numvar'
replace Diff_Alone_Peer=_se[Treatment_Peer] in `numvarse'
local numvar = `numvarse' + 1
}

The principle can be understood by looking at just a few commands.
local numvar=1
foreach var in ... {
replace `var'=. if miss_`var'==1
local numvarse = `numvar' + 1
replace varname="`var'" in `numvar'
*MEAN AND SD FOR CONTROL
quietly: sum `var' if Treated_All==0
replace Control=r(mean) in `numvar'
replace Control=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED
quietly:sum `var' if Treated_All==1
replace Treat=r(mean) in `numvar'
replace Treat=r(sd) in `numvarse'
*MEAN AND SD FOR TREATED WITH FRIEND
quietly:sum `var' if Treatment_Peer==1
replace Treat_Peer=r(mean) in `numvar'
replace Treat_Peer=r(sd) in `numvarse'
local numvar = `numvarse' + 1
}
numvar is initialised to 1. Within the loop, numvarse is set to 1 more than numvar.
The code then puts results for each of a series of variables in observations numvar and numvarse, including the name of the variable AND its mean and SD for various groups.
The key is to focus on
replace ... in ...
where the in qualifier specifies replace only in that observation.
Towards the end of the loop numvar is bumped yet again.
In short, results for the first variable named go in observations 1 and 2; for the second variable named go in observations 3 and 4; and so on.
Minimally, this process depends on
There being enough observations for this to be possible. I count 13 variables, so there must be at least 26 observations in the dataset, which seems overwhelmingly likely.
There being an understanding that the variables being written to are not aligned with any other variables.
Some might find #2 and even #1 poor ways of working. They would want results to be written to a new dataset or to a new frame.
The code is not state of the art: using xi with xtreg has been outdated practice since Stata 11 (2009), but still works for many purposes.
(Detail: The code overwrites values in variable X with missing if separately there is a variable miss_X indicating that variable X is missing. I don't think any remote observers can explain that.)

Related

What are the 2 bytes in the beginning of u1.AddressOfData in the memory mapped IAT of PE files?

So in order to parse the IAT table inside the memory of a PE process and get the names of imported functions, we have to iterate over functions of each module, and for each of them, use thunkData->u1.AddressOfData + 2 to get to the start of the Function name string (I don't want to start explaining what these pointers are because i assume anyone who knows the answer to this already knows this. and u1 is a predefined structure that Windows has and it always has AddressOfData in it)
so basically for every function inside the IAT, we have to use u1.AddressOfData + 2 to get the address of the start of the string, but i don't get what is the beginning 2 bytes of it? Microsoft documents don't explain this :
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format
(look for Delay Import Name Table)
i tried debugging it and looking what the value in the beginning of them are, and they were stuff like 0x8600 and 0xe700, so usually 1 byte of data and one byte of 00
so what is this?
in IMAGE_THUNK_DATAstructure, if function address yet not resolved and function not snap by ordinal - AddressOfData is point to PIMAGE_IMPORT_BY_NAME (this is visible if look in winnt.h and/or ntimage.h )
typedef struct _IMAGE_IMPORT_BY_NAME {
USHORT Hint;
CHAR Name[1];
} IMAGE_IMPORT_BY_NAME, *PIMAGE_IMPORT_BY_NAME;
the first 2 bytes before name - this is Hint index in AddressOfNames - exported names table (look for IMAGE_EXPORT_DIRECTORY ). this table of names sorted in alphabetical order for fast binary search function by name. Hint used for fast check, before drop into binary search. how it can used we can view in wrk src code
//
// Lookup Name in NameTable
//
NameTableBase = (PULONG)((PCHAR)DllBase + (ULONG)ExportDirectory->AddressOfNames);
NameOrdinalTableBase = (PUSHORT)((PCHAR)DllBase + (ULONG)ExportDirectory->AddressOfNameOrdinals);
//
// Before dropping into binary search, see if
// the hint index results in a successful
// match. If the hint index is zero, then
// drop into binary search.
//
HintIndex = ((PIMAGE_IMPORT_BY_NAME)NameThunk->u1.AddressOfData)->Hint;
if ((ULONG)HintIndex < ExportDirectory->NumberOfNames &&
!strcmp((PSZ)((PIMAGE_IMPORT_BY_NAME)NameThunk->u1.AddressOfData)->Name,
(PSZ)((PCHAR)DllBase + NameTableBase[HintIndex]))) {
OrdinalNumber = NameOrdinalTableBase[HintIndex];
}
else {
//
// Lookup the import name in the name table using a binary search.
//
however this index usually can be valid only in system images. if we build image yourself - in best cast this Hint can be valid only for one windows version (we got Hint from lib file) - when new version windows created - usually new export api added, because table is sorted alphabetical - indexes is [can] changed

Clarification for a loop to create interaction terms in Stata

I found the following question/answer that I think does what I would like to do: https://www.stata.com/statalist/archive/2009-09/msg00449.html
However, I am unclear what is going on in all of it, and would like to understand better. The code for the solution is as follows:
unab vars : var1-var30
local nvar : word count `vars'
forval i = 1/`nvar' {
forval j = 1/`=`i'-1' {
local x : word `i' of `vars'
local y : word `j' of `vars'
generate `x'X`y' = `x' * `y'
}
}
I do not understand what is going on in line 4 with the statement: ``=i'-1'.
The i refers to the number in the set {1,...,n}, but I do not understand what the equals or the -1 are doing. My assumption is that the -1 is somehow removing the own observation, but I am unclear. Any explanation would be appreciated.
Suppose you have local macro i that varies over a range and you want its value minus 1. You can always do this
local j = `i' - 1
and then refer to j. You can also do this on the fly:
`= `i' - 1'
Within
`= '
Stata will evaluate the expression, here
`i' - 1
and substitute the result of that expression in a command line.
You can do this with scalars too:
scalar foo = 42
and then refer to
`= foo'
However, watch out. Scalar names and variable names occupy the same namespace.
`= scalar(foo)'
disambiguates and arguably is good style in any case.

Defining Table or Array name as name from Previously Defined String in Matlab

So I would like to optimize my code such that I can look through an array such as
{'one','two','three'}
and create corresponding variables defined as tables or arrays
such as
one = table()
two = table()
three = table()
I am aware of the eval function however I would like to use this function in a loop s.t I allocate values to the new variable right after i create it
If I am understanding your question properly, given a cell array consisting only of strings, you wish to create variables in your workspace where each variable is declared as a string using the names from this cell array.
You could use eval, but I'm going to recommend something other than eval. eval should be avoided and instead of iterating those reasons, you can read Loren Shure's article on eval.
In any case, I would recommend you place these variables as dynamic fields inside a structure instead. Do something like this:
s = struct()
strs = {'one', 'two', 'three'};
for idx = 1 : numel(strs)
s.(strs{idx}) = table();
end
In this case, s would be a structure, and you can access the variable by the dot operator. In this case, you can access the corresponding variables by:
d = s.one; %// or
d2 = s.two; %// or
d3 = s.three;
If you want to place this into a function per se, you can place this into a function like so:
function [s] = createVariables(strs)
s = struct();
for idx = 1 : numel(strs)
s.(strs{idx}) = table();
end
This function will take in a cell array of strings, and outputs a structure that contains fields that correspond to the cell array of strings you put in. As such, you'd call it like so:
strs = {'one', 'two', 'three'};
s = createVariables(strs);
However, if you really really... I mean really... want to use eval, you can create your workspace variables like so:
strs = {'one', 'two', 'three'};
for idx = 1 : numel(strs)
eval([strs{idx} ' = table();']);
end
To place this into a function, do:
function [] = createVariables(strs)
for idx = 1 : numel(strs)
eval([strs{idx} ' = table();']);
end
However, be warned that if you run the function above, these variables will only be defined in the scope that the function was run in. You will not see these variables when the function exits. If you want to run a function so that the variables get defined in the workspace after you run the function, then eval is not the right solution for you. You should thus stick with the dynamic field approach that I talked about at the beginning of this post.
In any case, this will create one, two and three as workspace variables that are initialized to empty tables. However, I will argue with you that the first line of code is easier to read than the second piece of code, which is one of the main arguing points as to why eval should be avoided. If you stare at the second piece of code long enough, then you can certainly see what we're trying to achieve, but if you read the first piece of code, you can ascertain its purpose more clearly.

"Copy" does not create independent copy of Dynamic Array

With reference to on-line documentation found at http://docwiki.embarcadero.com/RADStudio/XE6/en/Structured_Types#Dynamic_Arrays.
It is quite clearly written that to make an independent copy of a dynamic array, use the Copy() function. Example code found at that link also illustrates that after the copy, if you change one array, it is not reflected into the other array as both are independent copies. It does not work for me, however. After the copy as well, if I change one array, the other array automatically changes to receive the same value thus implying that Copy() just did what X := Y would have done. Per documentation, "X := Y" and "X := Copy(Y)" are NOT same.
Here is my code:
type
TTestArray = array of TMyType; //Note: TMyType is a class and not a record.
var
X, Y, Z: TTestArray;
begin
SetLength(X, 1); //create first array
X[0].testString := 'Test Y';
Y := copy(X);
X[0].testString := 'Test Z'; //re-assign another value
Z := copy(X);
X[0].testString := 'Test X';
at this time, testString field should contain different text. So,
X[0].testString should be 'Test X'
Y[0].testString should be 'Test Y'
Z[0].testString should be 'Test Z'
However, all three just have 'Test X' as the value in testString thus implying that Copy() did not create independent copies of the array X. Rather, all three arrays are pointing to the same memory location.
Any way to reliably create independent copies of dynamic arrays (i.e. accomplish what I am trying to do above)?
NOTE (added later): TMyType is a class and not a record. So, per very helpful comments below, this is an expected behavior in case of a CLASS. So, how would I make independent copy of the X into Y and Z in this case?
NOTE 2: Removed "Bug" from subject line. Sorry, Embarcadero...
Note 3: TMyType has to be a class. I have no control over it. It is being created from a web service definition (WSDL) which exposes some functionality of PeopleSoft.
The problem is that you've defined TMyType as:
type
TMyType = class(SomeObject)
....
public
testString: string;
end;
When you create the initial TestArray, it does not get filled with the objects itself, but only with pointers to these objects.
What's going on...
The array thus looks like this:
Array contents of array contents of the heap
-------------+----------------------+-----------------------
X[0] |-> pointer_to_MyType1 |-> MyType1
X[1] |-> pointer_to_MyType2 |-> MyType2
When you copy the array this is what happens:
Array contents of array contents of the heap
-------------+------------------------------+-----------------------
Y[0] |-> copy_of_pointer_to_MyType1 |-> MyType1
Y[1] |-> copy_of_pointer_to_MyType2 |-> MyType2
What you would like to see...
This is what you expected to happen:
Array contents of array
-------------+-------------------
X[0] or Y[0] |-> (copy of)MyType1
X[0] or Y[0] |-> (copy of)MyType2
How to make that happen
However in order for that to be possible you have to define TMyType as:
type
TMyType = record
public
TestString: string;
end;
Now the record itself will be stored in the array and not a pointer to data stored elsewhere.
how would I make independent copy of the X into Y and Z in this case?
If you want to retain the use of classes then you'd have to write your own version of Copy; something like:
uses
System.SysUtils, system.classes, Generics.Collections;
type
TMyList<T: TPersistent, constructor > = class(TList<T>)
public
function CloneArray: TMyList<T>;
end;
implementation
function TMyList<T>.CloneArray: TMyList<T>;
var
i: integer;
temp: T;
begin
Result:= TMyList<T>.Create;
for i:= 0 to SizeOf(self) -1 do begin
temp:= T.Create;
temp.assign(self.items[i]);
Result.Add(temp);
end; {for i}
end;
Obviously the above code assumes that you can get away with using a parameterless constructor.
See also: Correct way to duplicate Delphi object
You should rather create a new question instead of changing or adding to your question/
To answer the second part of your question, the easiest thing would be to create you own CopyMyTypeArray function. Your code for the this would probably be something like this:
function CopyMyTypeArray(ASource: TTestArray): TTestArray;
var
newObject: TMyType;
begin
SetLength(Result, Length(ASource));
for cntr := Low(ASource) to High(ASource) do
begin
// You could put the lines below into a clone method
newObject := TMyType.Create;
newObject.testString := ASource[cntr].testString;
Result[cntr] := newObject;
end;
end;
Please note that you would need to free the objects if you free the dynamic array.
If you are happy with this solution please accept Johan's solution as that answers your initial question.
If you need independent copies of objects, the easiest way is to write a Copy Constructor. A copy constructer takes one argument - a reference to the existing object to be copied - and then initializes its internal fields to the same values as in the original object. This can be done with simple code, or using RTTI.
See also: Correct way to duplicate Delphi object
A configurable TMyTypeFactory (or TMyTypeBuilder) might be also an alternative solution.

Using Lua tables as 2-dimensional arrays

I'm a beginner Lua user, I attempt to create something in Lua by using Love2D libraries.
In the loading function I create a table and upload it with elements (which are numbers) for use it later as a multidimensional array.
function love.load()
Maximum_X = 32
Maximum_Y = 16
Start_X = 64
Start_Y = 32
MapTable = {} -- empty table
for i=1,Maximum_X*Maximum_Y do -- uploading table
table.insert(MapTable, 2)
end
end
Then I make a function that takes changes in the table. Because I'm just experimenting with tables, there's only one changed value. At least, I thought.
function KatamoriGen()
MapTable[4] = 3
end
function love.update(dt)
KatamoriGen()
end
After that, I print the elements of the table in a matrix with 32 coloumns and 16 rows. I see here that not only the 4th element of 1st row is changed, but also the 2nd element of 2nd row and 1st element of 4th row becomes 3.
It obviously means that Table[posX*posY] doesn't work neither since the result of the multiplication is a number like 4 and the operation would change every elements where
X coordinate + Y coordinate = posX*posY
is true. In the example code, the right side of this equation was 4.
A small question: why is it happening?
The main question is: how can I identify elements of MapTable exactly? How can I implement X and Y dimensions to Lua tables? to use them as two-dimensional arrays? Maybe table of tables?
EDIT: this is the drawing function:
function love.draw()
for j=1,16 do
for i=1,32 do
love.graphics.draw(Tileset[MapTable[j*Maximum_X + i]], Start_X + 32*(i-1), Start_Y + 32*(j-1))
end
end
end
Now it's clear for me that this is wrong and the right rule is MapTable[j*Maximum_X + i] but I get an error for it: "expected parameter type: expected userdata"
You can also use multi-dim tables. Something like:
local MapTable = {}
local Maximum_X, Maximum_Y = 32, 16
local Start_X, Start_Y = 64, 32
function love.load()
for y = 1,Maximum_Y do
local row = {}
for x = 1,Maximum_X do
table.insert(row,2)
end
table.insert(MapTable,row)
end
end
function love.draw()
for y,row in ipairs(MapTable) do
for x,idx in ipairs(row) do
love.graphics.draw(Tileset[idx], Start_X + 32*(x-1), Start_Y + 32*(y-1))
end
end
end
As Alex shows it, the problem was not with the way how table works, but rather that I displayed the numbers on a wrong way. Tables can be used as one-dimensional arrays, and everything fine then.
Even though it's not a solution for using them as 2-dimensional array, the main problem is solved.

Resources