Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: interactions


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: interactions
Date   Thu, 26 Jun 2003 15:27:15 +0100

David Airey

> Ernest,
> 
> I have not been able to use xi to create continuous*continuous 
> interactions on the fly. I don't think xi does this from the 
> description. It does cat*cont though. Desmat works with its own 
> notation, but I've written with pen and paper the following 
> program and 
> would like some comments. The program is meant to create 
> all two way 
> interactions for a given set of continuous variables (e.g., 
> a b c d):
> 
> * all two way interactions
> program define atwi
> syntax varlist (min 2 numeric)
> local t = total number of variables  <-- how do I get this?
> tokenize `varlist'
> for numlist j = 1(1)`t'-1 {
> 	for numlist k = `j'+1(1)`t' {
> 		generate ``j''x``k'' = ``j''*``k''
> 	}
> }
> end
> 
> given a command like:
> 
> . atwi a b c d
> 
> it should kind of go through the loops to get:
> 
> j	k	generate
> 1	2	axb
> 	3	axc
> 	4	axd
> 2	3	bxc
> 	4	bxd
> 3	4	cxd
> 
> and then I could type
> 
> atwi a b c d
> regress Y a b c d a* b* c*
> 
> to get what I want, which is really a general linear test 
> on adding all 
> nway interaction at a certain level.
> 
> Any comments welcome before I try it out with a do file.
> 

Program -selectvars- on SSC yields tuples of variable 
names from a list. That's one of the parts of doing this
more generally. 

One of the trickier parts is getting new varnames which not 
only are new but also not too long. David's program doesn't 
(really try to) tackle this. (Neither does -selectvars-.) 

Anyway, he asked a question: 

------------ David's code
* all two way interactions
program define atwi
syntax varlist (min 2 numeric)
local t = total number of variables  <-- how do I get this?
tokenize `varlist'
for numlist j = 1(1)`t'-1 {
 	for numlist k = `j'+1(1)`t' {
 		generate ``j''x``k'' = ``j''*``k''
 	}
}
end
----------------------

This is a step or two further on, without 
solving the naming problem: 

* all two way interactions
program atwi
	syntax varlist(min=2 numeric)
	local t : word count `varlist'
	tokenize `varlist'
	forval j = 1/`=`t'-1' {
		forval k = `=`j'+1'/`t' {
	 		generate ``j''x``k'' = ``j''*``k''
	 	}
	}
end


Nick 
n.j.cox@durham.ac.uk 


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index