regress with a constraint/condition - command

I want to execute a regression but I need a condition/constraint in the command but mine doesn't work. My dependent variable is COMP_STD and my independent is BGroup. BGroup has the following values: 1 2 3 4 5.
I want to run 5 different regressions with the variable BGroup, so that this variable equals 1, 2, and so on. This is the syntax that I tried:
regress COMP_STD if inrange (BGroup, 1)
but Stata says inrange not found so I guess it has to be another syntax which I can't find.

inrange() shouldn't be presented as inrange (whatever), i.e. with a following space. There is a hint that Stata can't understand you; otherwise why is it asking about inrange? The reason for that puzzlement is that --when separated from its arguments -- a bare inrange looks like the name of a variable (or scalar), and you have no such variable (or scalar).
You didn't get that far, but the inrange() call was illegal for another reason. It needs three arguments, e.g. if inrange(BGroup, 1, 1).
But if BGroup == 1 is much simpler.
Note that regress is irrelevant here as the problem is entirely about selecting observations. That said, statsby is a command possibly useful here for getting several regressions at once.

Related

How to automatically convert a Matlab script into a Matlab function?

My problem is the following:
I have very many (~1000) mutually calling Matlab scripts, which are very poorly written, regularly damage each other's environments and generally became unmanageable.
One of the reasons I even got this problem is that I need to write a testsuite covering a big part of them. Luckily, for most of them the main criterion of 'correctness' is 'they don't crash'.
Just running them one by one in a loop is generally not an option, because they regularly call clear classes, close all, clc, shadow built-in functions and operators, et cetera.
So my original aim was to find a way to run a matlab script in sort of an 'isolated environment', but I didn't find a good way to do it. (Suggestions welcome, but it is not the main question.)
Since I will need to convert them all to functions anyway, I am looking for some way to do it auto-magically, or at least semi-automatically.
What I can mean semi-automatically:
Just add a line function varargout = $filename( varargin ) as the first line of the file, and end as the last one. This will at least make them runnable as functions with feval and all such functions and (more importantly) prevent them from damaging the test-runner.
Do point 1 and scan the file for referencing undeclared variables and add them as function arguments. This should be also doable, since the names of the variables are known. This will not help identifying output variables, but will still be a lot of help. For example, we could pack the whole workspace into one big output structure.
Do a runtime version of point 2. This way the 'magical converter' can actually track execution environments (workspaces) and identify which variables are implicitly used as 'input arguments' of a script, and which would be later used 'output arguments'. This option looks EXPHARD, but for a small number of calls should be not too bad in practice.
Point 1 I can implement myself using sed, as I also can get rid of all clear classes and clc, but the options 2 and 3 seem much harder. Is there anything at least remotely resembling options 2 or 3?

How to use OR condition in LibreOffice?

I am trying to use the formula below to set conditions in LibreOffice but I keep getting an error. What am I doing wrong with the statement below:
=IF(G2<=2,'negative',IF(OR(G2>2 & G2<=3,'neutral',IF(OR(G2>=4,'positive))))))
Thanks
It seems, that in your formula is missing the last ':
'positive))))))
should be 'positive'))))))
Also the
&
is string-concatenation in LibreOffice, so you need here the equivalent to OR() and that is AND().
But you can simplify your formula to
=IF(G2<=2,'negative',IF(AND(G2>2,G2<=3),'neutral','positive'))
The first test is if the number is lower than 2 (negative),
the second test is if the number is between 2 and 3 (neutral)
and then there is no further test needed as it is the only remainig possiblity.
For a different locale, a slightly shorter, and I'd say simpler, version that also avoids the need for OR/AND:
=IF(G2<=2,"negative",IF(G2<=3,"neutral","positive"))
Once <=2 first test is handled (either but outputting negative or by proceeding to the 'result if FALSE') there is no longer the possibility of 2 or less, so the AND is not necessary.
The above though does fill a gap left by OP between 3 and 4.

Variable output hash function

I know that there are hash functions that from a variable length input can give a fixed output. To take the simplest one, using the module of ten no matter how big is the input number I will always get an output between 0 and 9.
I need to do have from an unknown password, a variable length output. My first thought was to use the module, increasing the prim number as much as many digits I need to have as output.
My problems are:
I must handle short passwords as well as I would with long passwords;
I don't know how long should the output be before writing the program, and even though I would know after the user has set the password I may need to change it if he modifies the file.
My first thought was using a simple function and modify it based on my needs.
If I have to hash 123 but I need to have 5 characters as output, that's what I would do:
I add 2 zeros on the right, changing the input to 12300;
I take the lowest 5 digits prime number (10007);
And I then I have my hash doing 12300 % 10007 = 02293.
But since I would probably need output in the order of hundreds if not thousands I'm pretty sure module is not the solution to my problem.
I could also try to create my own hash function, but I have no idea how to verify if it works or if it's trash.
Are there some common solutions in literature for this kind of problem?

At which lines in my MATLAB code a variable is accessed?

I am defining a variable in the beginning of my source code in MATLAB. Now I would like to know at which lines this variable effects something. In other words, I would like to see all lines in which that variable is read out. This wish does not only include all accesses in the current function, but also possible accesses in sub-functions that use this variable as an input argument. In this way, I can see in a quick way where my change of this variable takes any influence.
Is there any possibility to do so in MATLAB? A graphical marking of the corresponding lines would be nice but a command line output might be even more practical.
You may always use "Find Files" to search for a certain keyword or expression. In my R2012a/Windows version is in Edit > Find Files..., with the keyboard shortcut [CTRL] + [SHIFT] + [F].
The result will be a list of lines where the searched string is found, in all the files found in the specified folder. Please check out the options in the search dialog for more details and flexibility.
Later edit: thanks to #zinjaai, I noticed that #tc88 required that this tool should track the effect of the name of the variable inside the functions/subfunctions. I think this is:
very difficult to achieve. The problem of running trough all the possible values and branching on every possible conditional expression is... well is hard. I think is halting-problem-hard.
in 90% of the case the assumption that the output of a function is influenced by the input is true. But the input and the output are part of the same statement (assigning the result of a function) so looking for where the variable is used as argument should suffice to identify what output variables are affected..
There are perverse cases where functions will alter arguments that are handle-type (because the argument is not copied, but referenced). This side-effect will break the assumption 2, and is one of the main reasons why 1. Outlining the cases when these side effects take place is again, hard, and is better to assume that all of them are modified.
Some other cases are inherently undecidable, because they don't depend on the computer states, but on the state of the "outside world". Example: suppose one calls uigetfile. The function returns a char type when the user selects a file, and a double type for the case when the user chooses not to select a file. Obviously the two cases will be treated differently. How could you know which variables are created/modified before the user deciding?
In conclusion: I think that human intuition, plus the MATLAB Debugger (for run time), and the Find Files (for quick search where a variable is used) and depfun (for quick identification of function dependence) is way cheaper. But I would like to be wrong. :-)

How to avoid a meta argument warning in SICStus SPIDER?

This is probably related to a comp.lang.prolog-discussion.
I'm getting several warnings like this using Eclipse with the SICStus SPIDER:
The plain meta argument (Y) is passed as a closure argument
(with 0 suppressed arguments) to the callee.
Here is a code sample:
% Prologs set_of is baroque %% RS-140614 130sec runtime vs. 28sec runtime
:- meta_predicate set_of(+,:,+) .
set_of(X,Y,Z):- %%
setof(X,Y^Y,Z),!; %% Trick to avoid alternatives
Z=[]. %% What is wrong with empty sets ?
How can I get rid of the SPIDER warnings?
I'm not really interested in simply suppressing the warnings.
I'm using the latest version of SPIDER IDE (0.0.51), and SICStus Prolog 4.2.3.
There are several issues in the code you show.
Bad meta argument
First, the built-in predicate setof/3 has the following properties:
?- predicate_property(setof(A,B,C),P).
P = (meta_predicate setof(?,0,?))
; P = built_in
; P = jittable.
which closely corresponds to the ISO declarations in ISO/IEC 13211-1:
8.10.3.2 Template and modes
setof(?term, +callable_term, ?list)
The second argument is a goal to be executed by call/1. No extra arguments are needed. This is what the 0 tells us.
On the other hand, your code you show contains a different meta predicate declaration:
:- meta_predicate(set_of(+,:,+)) .
Here, the second argument is a :. In SICStus, YAP, and SWI, the : means: This argument will be automatically qualified with the current module, such that the module information can be passed further on. Think of asserta(:). Here, the argument is not a goal but a clause.
So what you need to fix this, is to replace : by 0. And you might indicate this fact in the variable name used. That is, Goal_0 for call(Goal_0), Goal_1 for call(Goal_1, Arg1), Goal_2for call(Goal_2, Arg1, Arg2) etc.
Bad modes
The + in the first and third argument is inappropriate. The 3rd argument is commonly an uninstantiated variable to be unified with the resulting list.
Prolog's setof/3 baroque?
% Prologs set_of is baroque
The comment probably wants to say that setof/3 contains superfluous ornaments. In fact, setof/3 is much more versatile than mentioned set_of/3. Take this recent question or that. Often you first think about a very specific situation. Say, you want the list of actors of a particular movie. Then, later on you want to ask what movies there are. It is this generalization which works very smoothly with setof/3 whereas it is extremely complex if you do not have it.
Another very useful way to use setof/3 is when you want to eliminate redundant answers:
?- (X=2;X=1;X=2).
X = 2
; X = 1
; X = 2.
?- setof(t, (X=2;X=1;X=2), _).
X = 1
; X = 2.
Try to emulate that efficiently.
Runtime overheads
They are next to negligible. If you really believe that there are overheads, simply use setof/3 with a single goal. In this manner preprocessing is next to naught.