Hello,

The other problem with the boxplot question is that it's ill defined. Those data[,1], ..., data[, n] variables, what are they? All of them numeric? If so, then where is the factor?

Some time ago there was a question on multiple boxplots with cut() defining the factor. I wrote a beneral purpose function as an answer:

https://www.stat.math.ethz.ch/pipermail/r-help/2012-July/317991.html

# Example use.
n <- 200
set.seed(1920)
ID <- rep(LETTERS[1:4], n/4)
x <- matrix(runif(3*n, max=rep(c(2, 1, 1.5), each=n)), ncol=3)
DF <- data.frame(ID=ID, x=x)

cols <- grep("x", names(DF)) # something variable
multi.boxplot(DF[, cols], DF$ID, col = rainbow(3))


Hope this helps,

Rui Barradas

Em 08-08-2012 14:43, S Ellison escreveu:
First thing; are you trying to fit a model specified as

y ~ X + X^2 + X^3 ?

... because if you are you're unlikely to get anything useful. That uses 
formula syntax in which ^ does not have the arithmetic power meaning; see 
section 11.1 'Defining statistical models; formulae' in 'an introduction to R' 
in your HTML help system. This formula only specifies one term, X, so all 
you'll get is the result of

y ~ X

If you wanted to fit a polynomial the hard (and not generally good) way you'd 
have to do

y ~ I(X )+ I(X^2) + I(X^3)

Next, in your line
  fit <- lm(Y~conditional.argument)

you have asked lm to fit Y to a character string formed by paste(). That is also unlikely 
to be useful. You would need to create the complete formula as a string (eg 
"Y~X+I(X^2)+I(X^3)" ) and then use as.formula to convert that to a formula lm 
can use.

Then there's the unnecessary loop. You can get your formula string without a 
loop from an integer 'degree' using

formstring <- paste("Y ~ ", paste("I(X^",1:degree,")" , sep="", collapse=" + "))

And then you can convert formstring to a formula object and use the formula 
object in lm:
form <- as.formula(formstring) #be careful with this; it looks for the terms in 
the current environment...
lm(form)


Or, if 'degree' were a vector (say
degree<-c(1, 3, 5)
formstring <- paste("Y ~ ", paste("I(X^",degree,")" , sep="", collapse=" + "))
and so on.

I am having this problem as well when I want to do a boxplot
with variable number of groups to compare.
I don't know what you're trying to achieve with boxplot, but if you gave 
boxplot itself your vectors it would plot them by itself:

x<-rnorm(17)
y<-runif(23)
z<-c(x,y)

boxplot(x,y,z)

Incidentally, this:
conditional.argument <- data[,1], data[,2], ...,data[,nvariables]          #
is not a valid assignment in R, so it cannot be the code you used. And using 
something called 'data' (or any other object) in a function that does not have 
an named argument by the same name is asking for trouble; there is no telling 
where it will get 'data' from but there is a very good chance it will sometimes 
be from somewhere you don't expect.


S Ellison
-----Original Message-----
I am sure that my approach is kind of stupid but I cannot
think of another way to do it. I apologize...

foo <- function(..., degree=..){
   degree<-3 # example
conditional.argument <- rep(NA,degree)
   for(i in 1:(degree-1)) conditional.argument[i] <-
paste("X^",i,"+",sep="")
   conditional.argument[degree] <- paste("X^",degree,sep="")
   conditional.argument <- paste(conditional.argument,collapse="")
conditional.argument
#  "X^1+X^2+X^3"

   fit <- lm(Y~conditional.argument)
   ...
   return(fit)
}

I know that there is the poly() function which I am not looking for.
I am having this problem as well when I want to do a boxplot
with variable number of groups to compare.

multiple.boxplot <- function(...,nvariables){
conditional.argument <- data[,1], data[,2],
...,data[,nvariables]          #
should look like that in the end
boxplot(conditional.argument)
}

Can anyone give me a hint?
Daniel Hoop




--
View this message in context:
http://r.789695.n4.nabble.com/Pass-Conditional-loop-argument-i
nto-a-function-tp4639580.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org 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.

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
R-help@r-project.org 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-help@r-project.org 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.

Reply via email to