通过mapply将多个参数传递给公式函数

时间:2015-03-02 00:08:20

标签: r

我想进行两次黄土回归。数据作为包含两个元素的列表提供。每个元素本身都包含一对列(x和y用于回归),我想运行黄土回归。我想通过专门使用申请家庭来做到这一点。然而,黄土回归采用公式表达式y~x,我相信你不能像公式格式那样直接引用x和y,就像非公式函数那样,变量可以通过mapply提供。

X <- c(3,4,3,2,3,4,5,6,7,7,6,5,4,3,3,5,3,6,3,5,6,3,6,3,4,5,5,4,3,4,5,3,5,5,4)
Y <- c(3,2,1,3,4,2,1,2,3,5,4,3,2,1,1,3,4,5,6,7,6,5,4,3,2,3,4,3,4,2,4,3,NA,NA,NA)

mydata<-data.frame(X,Y)

L <- seq(1:length(mydata))
n <- function(x) length(na.omit(mydata[,x]))
n <- lapply(L,n)

# sequence each (Variable time)
x <- function(x) seq(1:n[[x]])
x <- lapply(L,x)
y <- function(x) na.omit(mydata[,x])
y <- lapply(L,y)

# create a list with pairs of columns each 
Data <- function (p) data.frame(y[[p]], x[[p]])
Data <- lapply(L,Data)

# In a writen function you would do (where p will correspond to the sequence of L and d is the number of columns in each element) and use mapply passing the arguments 

d <- seq(1:length(mydata))
p <- seq(1:length(Data))
W1<-expand.grid(p=p,d=d)

# However the formula framework y ~ x for loess does not allow to pass multiple variable arguments to x and y and use mapply to do so which I would like to do as to automate this process. I wrote in the same format as x and y variables will be passed to a non formula function which does not work.     

mapply(function(p,d) ((y ~ x, span = 0.75, degree = 2,parametric = FALSE, drop.square = FALSE, normalize = FALSE,family = c("gaussian")),W1$p,W1$d) 

我想知道如何使用mapply函数将不同的变量传递给loess函数。

0 个答案:

没有答案