Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

[R] r: functions

allan clark

2005-02-24

Replies:

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@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
©2008 r-help.com - Jax Systems, LLC, U.S.A.