Re: [R] for loop not working in function

2006-10-11 Thread Petr Pikal
Hi

You definitely shall consult an introduction manuals you can find in 
doc directory of your R installation. Good starting point is also 
http://www.r-project.org/ and its documentatation section.

boxcox - function(x,min,max,step) {
lambda - seq(min,max,step) # you set lambda1
s - length(lambda)

for (lambda in 1:s) # here you expect lambda is different from 
# lambda1 and you make all computation with this new value of lambda
# you need to use also curly braces to include all rows between them
# into one cycle

n - nrow(x)
if(lambda ==0) xL - log(x) else xL - ((x^lambda) - 1)/lambda
xLbar - mean(xL)
t1 - (-n/2)* log((1/n)*sum((xL -  xLbar)^2))
t2 - (lambda - 1)*sum(log(x))
l= t1 + t2
l
}



On 11 Oct 2006 at 0:28, Dale Steele wrote:

Date sent:  Wed, 11 Oct 2006 00:28:21 -0400
From:   Dale Steele [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Subject:[R] for loop not working in function

 I'm trying to write a small function (below) to compute Box  Cox
 transformations of x for arbitrary values of lambda.  I'd like to
 specify a range of values for lamba (min,max,step) and am having
 trouble getting the for loop to work.  Suggestions?
 
 Any pointers to resources for learning to write functions in R for
 neophyte programmers?  Thanks.  --Dale
 
 
 boxcox - function(x,min,max,step) {
 lambda - seq(min,max,step)
 s - length(lambda)
 for (lambda in 1:s)
 n - nrow(x)
 if(lambda ==0) xL - log(x) else
 xL - ((x^lambda) - 1)/lambda
 xLbar - mean(xL)
 t1 - (-n/2)* log((1/n)*sum((xL -  xLbar)^2))
 t2 - (lambda - 1)*sum(log(x))
 l= t1 + t2
 l
 }
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html and provide commented,
 minimal, self-contained, reproducible code.

Petr Pikal
[EMAIL PROTECTED]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] for loop not working in function

2006-10-11 Thread David Barron
I'd suggest looking at the boxcox function in the MASS package, which might
show you how to change your code.

It would be easier to diagnose the problem if you included a small example
showing the errors.  Some possibilities are that if x is a vector, nrow(x)
will return NULL,  that you are using lambda as the index variable in the
for loop, so the sequence generated in the second line is getting
overwritten, and the for loop isn't enclosed in braces, so only the first
line will be counted as being in the loop.

HTH,

David

On 11/10/06, Dale Steele [EMAIL PROTECTED] wrote:

 I'm trying to write a small function (below) to compute Box  Cox
 transformations of x for arbitrary values of lambda.  I'd like to
 specify a range of values for lamba (min,max,step) and am having trouble
 getting the for loop to work.  Suggestions?

 Any pointers to resources for learning to write functions in R for
 neophyte programmers?  Thanks.  --Dale


 boxcox - function(x,min,max,step) {
 lambda - seq(min,max,step)
 s - length(lambda)
 for (lambda in 1:s)
 n - nrow(x)
 if(lambda ==0) xL - log(x) else
 xL - ((x^lambda) - 1)/lambda
 xLbar - mean(xL)
 t1 - (-n/2)* log((1/n)*sum((xL -  xLbar)^2))
 t2 - (lambda - 1)*sum(log(x))
 l= t1 + t2
 l
 }

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
=
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] for loop not working in function

2006-10-10 Thread Dale Steele
I'm trying to write a small function (below) to compute Box  Cox 
transformations of x for arbitrary values of lambda.  I'd like to 
specify a range of values for lamba (min,max,step) and am having trouble 
getting the for loop to work.  Suggestions?

Any pointers to resources for learning to write functions in R for 
neophyte programmers?  Thanks.  --Dale


boxcox - function(x,min,max,step) {
lambda - seq(min,max,step)
s - length(lambda)
for (lambda in 1:s)
n - nrow(x)
if(lambda ==0) xL - log(x) else
xL - ((x^lambda) - 1)/lambda
xLbar - mean(xL)
t1 - (-n/2)* log((1/n)*sum((xL -  xLbar)^2))
t2 - (lambda - 1)*sum(log(x))
l= t1 + t2
l
}

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.