# [R] R question

Faheem Mitha faheem at email.unc.edu
Thu Dec 16 21:19:21 CET 1999

Dear Dr. Ihaka, Thank you for your reply. The example below was merely an
example. The reality is more complex. I have a function called h which
takes a scalar argument and returns an lm object. In the loop I was hoping
h(l[i]) would be interpreted as h of the scalar argument, but instead I
get:

I will probably give up on this method for the time being, and try another
method, since I pressed for time.

My failed script is below in case anybody is willing to take a look.
Sincerely, Faheem Mitha.

*************************************************************************************
#I'm regressing the variable sal against some other variables (sallag,fresh,per,yr)
#This script is to use the method of constructed variables to get a
#transformation h from a family indexed by a parameter lambda such that
#the model h(sal) ~ other variables maximises the likelihood over all such
#h.

attach(salinity)

# sal, fresh, per, yr all vectors of same length

yr <- yr - 1971

#saldot geometric mean of elements of sal vector

saldot<- exp((1/length(sal))*log(prod(sal)))

w0 <- saldot*log(sal)*( 0.5*log(sal) - log(saldot) )
h0 <- saldot*log(sal)

trans0<-lm(h0 ~ w0+sallag+fresh+per+yr)

summary(trans0)
dffits(trans0)
dfbetas(trans0)

l1 <- - coef(trans0)

#Define functions h and w for recursion (both functions of lambda)

h <- function(l)
{
x <- (sal^l - 1)/( l*(saldot)^(l-1) )

x
}

w <- function(l)
{
x <- ( (sal^l)*log(sal) - (sal^l - 1)*( (1/l) + log(saldot) ))/
( l*(saldot)^(l-1) )

x
}

# Here is the for loop

for(i in 1:8){trans[i]<-lm( h(l[i]) ~ w(l[i])+sallag+fresh+per+yr)

l[i+1] <-  l[i] - coef(trans[i])}

#Trying to make the for loop equivalent to the set of statements below

trans1<-lm( h(l1) ~ w(l1)+sallag+fresh+per+yr)

l2 <-  l1 - coef(trans1)

trans2<-lm( h(l2) ~ w(l2)+sallag+fresh+per+yr)

l3 <- l2 - coef(trans2)

trans3<-lm( h(l3) ~ w(l3)+sallag+fresh+per+yr)

l4 <- l3 - coef(trans3)

trans4<-lm( h(l4) ~ w(l4)+sallag+fresh+per+yr)

l5 <- l4 - coef(trans4)

trans5<-lm( h(l5) ~ w(l5)+sallag+fresh+per+yr)

l6 <- l5 - coef(trans5)

trans6<-lm( h(l6) ~ w(l6)+sallag+fresh+per+yr)

l7 <- l6 - coef(trans6)

trans7<-lm( h(l7) ~ w(l7)+sallag+fresh+per+yr)

l8 <- l7 - coef(trans7)

#Now try usual regression with h_{\lambda}(y) for \lambda = l8

trans <- lm(h(l) ~ sallag+fresh+per+yr)

*************************************************************************************

On Fri, 17 Dec 1999, Ross Ihaka wrote:

> On Thu, Dec 16, 1999 at 02:41:25PM -0500, Faheem Mitha wrote:
> > I have the following question, which is elementary but I am unable to
> > In a for(i=10) loop, I am trying to represent the 10 1-dimensional vectors
> > l1, l2,... l10 by some expression that will run through these values.
> > ie. soppose I want to add l1 + ... + l10
> > I could go
> >
> > x <- 0
> > for(i in 1:10){ x <- x+ l(i)}
> >
> > This should return x to be the sum of the 10 li's for i from 1 to 10
> > except of course I'm doing something more complicated.
> > But l(i) of course does not serve to represent l1 etc? What should I do?
>
> Subsetting in S (and hence R) uses [] for subsetting.  What you need is
>
> 	x <- 0
> 	for(i in 1:10) x <- x + l[i]
>
> (If the values are really in a vector, x <- sum(l) is rather faster).
>
> 	Ross
>

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._