[R] Re: r: functions
Clark Allan
Allan at STATS.uct.ac.za
Thu Feb 24 14:01:56 CET 2005
hi all
i worked it out. the following code seems to work. thanx for those who
replied to previous questions.
simple1<-function(xdata,ydata)
{
DATA<-data.frame(ydata,xdata)
ofit<-summary(lm(ydata~.,data=DATA))
list(ofit)
}
simple1(x,y)
> simple1(x,y)
[[1]]
Call:
lm(formula = ydata ~ ., data = DATA)
Coefficients:
(Intercept) x1 x2
-6 21 -5
***
allan
Clark Allan wrote:
>
> 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
More information about the R-help
mailing list