# [Rd] Programming using formulas

Klaus Nordhausen klaus.nordhausen at uta.fi
Thu Jul 9 20:26:02 CEST 2009

```Dear R experts,

I'm planning to write some kind of multivariate regression function
where I would like to use a formula method.

My question is if there is anywhere some detailed introduction how to
program formulas in R?

A bit more about what I need:

I would like to start implementing a multivariate hierarchical
regression model and to keep it simple in the beginning I would restrict
to only one hierarchical level and one categorical explanatory variable
or just the intercept (one sample case).

For example I collect three numeric scores y1 , y2 and y3 from students
and would like to get the gender effect x while taking into account that
the students belong to different classes z.

I have then a formula in mind like
y1 + y2 + y3 ~ x | z
or
Y ~ x | z

For my internal functions I would need to be able to get from the
formula three matrices:
- Y the matrix of responses (should be checked to be numeric)
- X a model matrix (where factors should be dummy coded)
- Z matrix indicating to which cluster the subject belongs

And I would like to program that in such a flexible way that it would be
later possible to increase the level of hierarchies, for instance that I
consider also different schools within which I have different classes
and that x could become numeric and also several variables and so on.

What would be then the best way to handle that the response should be
multivariate and numeric? What is the best way to define the
hierarchical structure, is it the "|" and how can I restrict the number
of hierarchical levels?
And how to use in this context, the data, subset, weights and na.action
arguments?

In case you are wondering what I plan to implement, one references for
example is:

Haataja, R., Larocque, D., Nevalainen, J. and Oja, H. (2009). A weighted
multivariate signed-rank test for cluster-correlated data. Journal of
Multivariate Analysis, 100, 6, 1107-1119.

Best wishes,

Klaus

--
Klaus Nordhausen
Researcher
Tampere School of Public Health
FIN-33014 University of Tampere

phone:	+358 3 3551 4153
fax:	+358 3 3551 4150
e-mail:	Klaus.Nordhausen at uta.fi

```