在r中调用函数本身内部的函数

在r中调用函数本身内部的函数,r,function,call,match,lm,R,Function,Call,Match,Lm,我想通过自己创建来复制函数lm()。我已经编写了查找系数、vcov、sigma和df的代码,但是我不知道如何在函数本身内部调用我创建的函数(即“linMod”)。我知道我应该使用“match.call”,但我从来没有使用过它,我对它的工作原理有点困惑 linMod <- function(formula,data){ mf <- model.frame(formula=formula, data=data) x <- model.matrix(attr(mf, "terms"

我想通过自己创建来复制函数lm()。我已经编写了查找系数、vcov、sigma和df的代码,但是我不知道如何在函数本身内部调用我创建的函数(即“linMod”)。我知道我应该使用“match.call”,但我从来没有使用过它,我对它的工作原理有点困惑

linMod <- function(formula,data){

mf <- model.frame(formula=formula, data=data)
x <- model.matrix(attr(mf, "terms"), data=mf)
y <- model.response(mf)

## compute (x'x)^(-1)
x_1 <- solve(crossprod(x,x))
## compute beta
beta <- tcrossprod(x_1,x)%*%y
## calculate degrees of freedom
df <- nrow(x)-ncol(x)
## calculate sigma^2
sig <- y-(x%*%beta)
sigma2 <- crossprod(sig,sig)/df
sigma <- sqrt(sigma2)
##compute vcov
vcov <- as.vector(sigma2)*x_1

# I create a call here -> match.call(), right?
return(list("coefficients" = beta, 
          "vcov" = vcov, 
          "df" = df, 
          "sigma" = sigma,
          "call" = #call of the linMod function itself))


}
与lm()函数相同。

请尝试以下操作:

call = match.call()

通过不带括号地写函数名,您始终可以看到函数的代码,例如
lm
。然后你可以看到事情是如何实现的。
lm
的第三行是
cl谢谢!我没想过。。
call = match.call()