[R] r: functions
Clark Allan
Allan at STATS.uct.ac.za
Thu Feb 24 13:46:10 CET 2005
hi
i still get the same error. i must be doing something wrong. i have
typed in all of the steps i used.
D
x1 x2 y
1 1 1 10
2 2 6 6
3 3 10 7
x<-D[,1:2]
y<-D[,3]
> x
x1 x2
1 1 1
2 2 6
3 3 10
> y
[1] 10 6 7
> simple1 <- function(xdata, ydata){
+ ofit <- substitute(lm(ydata~xdata))
+ list(eval.parent(ofit))
+ }
simple1(xdata=x, ydata=y)
Error in model.frame(formula, rownames, variables, varnames, extras,
extranames, :
invalid variable type
I STILL GET THE ERROR. WHAT AM I DOING WRONG?
Dimitris Rizopoulos wrote:
>
> There was a similar question about one week ago regarding the use of
> "table(x,y)". One approach could be to use the following:
>
> simple1 <- function(xdata, ydata){
> ofit <- substitute(lm(ydata~xdata))
> list(eval.parent(ofit))
> }
> #############
> simple1(xdata=x, ydata=y)
> lm(y~x)
>
> I hope it helps.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/16/336899
> Fax: +32/16/337015
> Web: http://www.med.kuleuven.ac.be/biostat/
> http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
>
> ----- Original Message -----
> From: "Clark Allan" <Allan at stats.uct.ac.za>
> To: <r-help at stat.math.ethz.ch>
> Sent: Thursday, February 24, 2005 10:16 AM
> Subject: [R] r: functions
>
> > hi all
> >
> >
> > i have a function that uses two inputs, say xdata and ydata. An
> > example
> > is the following,
> >
> > simple1<-function(xdata,ydata)
> > {
> > ofit<-lm(ydata~xdata)
> > list(ofit)
> > }
> >
> > say i use arbitray number for xdata and ydata such that
> >
> > D =
> > x1 x2 y
> > 1 1 10
> > 2 6 6
> > 3 10 7
> >
> >
> > x<-D[,1:2]
> >
> > and
> >
> > y<-D[,3]
> >
> > if one uses these inputs and rund the program we get the following:
> >
> >>simple(xdata=x,ydata=y)
> > Error in model.frame(formula, rownames, variables, varnames, extras,
> > extranames, :
> > invalid variable type
> >
> > why does this happen!
> >
> > i can get results if i change the program as follows:
> >
> > simple2<-function(xdata,ydata)
> > {
> > ofit<-lm(as.matrix(ydata)~as.matrix(xdata))
> > list(ofit)
> > }
> >
> > but then the variable names, if they exist, are not preserved. how
> > can i
> > preserve these names.
> >
> > the results are now:
> >
> >> simple2(xdata=x,ydata=y)
> > [[1]]
> >
> > Call:
> > lm(formula = as.matrix(ydata) ~ as.matrix(xdata))
> >
> > Coefficients:
> > (Intercept) as.matrix(xdata)x1 as.matrix(xdata)x2
> > -6 21 -5
> >
> > i've tried converting xdata and ydata to data frames but i still get
> > errors.
> >
> > simple3<-function(xdata,ydata)
> > {
> > xdata<-as.data.frame(xdata)
> > ydata<-as.data.frame(ydata)
> > ofit<-lm(ydata~xdata)
> > list(ofit)
> > }
> >
> > i.e.
> >
> >> simple3(xdata=x,ydata=y)
> > Error in model.frame(formula, rownames, variables, varnames, extras,
> > extranames, :
> > invalid variable type
> >
> > please help!
> >
> > thanking you in advance
> >
> > ***
> > allan
>
> --------------------------------------------------------------------------------
>
> > ______________________________________________
> > R-help at 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
More information about the R-help
mailing list