probably D (it would be better not to use D since this is reserved for an R function) is a data.frame and thus also

x <- D[,1:2]

is a data.frame!

try this:

D <- data.frame(x1=1:3, x2=c(1,6,10), y=c(10,6,7)) # your data.frame
D. <- data.matrix(D)
x <- D.[,1:2]
y <- D.[,3]
simple1 <- function(xdata, ydata){
    ofit <- substitute(lm(ydata~xdata))
    list(eval.parent(ofit))
}
##################
simple1(xdata=x, ydata=y)


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" <[EMAIL PROTECTED]>
To: "Dimitris Rizopoulos" <[EMAIL PROTECTED]>
Cc: <r-help@stat.math.ethz.ch>
Sent: Thursday, February 24, 2005 1:46 PM
Subject: Re: [R] r: functions



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" <[EMAIL PROTECTED]>
To: <r-help@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@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

______________________________________________ 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

Reply via email to