Skip to content
Prev 4066 / 398506 Next

R question

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:

Error in h(l[i]) : Object "l" not found

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.

salinity <-read.table('salinity1.d')
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)[2]

#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])[2]}


#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)[2]

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

l3 <- l2 - coef(trans2)[2]

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

l4 <- l3 - coef(trans3)[2]

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

l5 <- l4 - coef(trans4)[2]

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

l6 <- l5 - coef(trans5)[2]

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

l7 <- l6 - coef(trans6)[2]

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

l8 <- l7 - coef(trans7)[2]

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

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

*************************************************************************************
On Fri, 17 Dec 1999, Ross Ihaka wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._