gpt4 book ai didi

r - 如何将公式作为参数传递给 r 中的函数?

转载 作者:行者123 更新时间:2023-12-05 05:14:21 25 4
gpt4 key购买 nike

如何在 R 中将公式作为参数传递?

下面的代码适用于前两种情况,但是当我传入公式时,出现错误:Error in model.frame.default(formula = formula, weights = weights, na.action = na .omit, : 变量“(weights)”的无效类型(闭包)

makeModel<-function(formula,weights) {
m <- lm(formula, na.action = na.omit, weights = weights)
return(m);
}
run<-function(t) {
f<-formula(t$y~t$x+t$r)
m <- lm(t$y~t$x+t$r, na.action = na.omit, weights = t$size)
m <- lm(f, na.action = na.omit, weights = t$size)
m <- makeModels(f,t$size)
}
l<-20
x<-seq(0,1,1/l)
y<-sqrt(x)
r=round(runif(n=length(x),min=0,max=.8))
n<-1:(l+1)
size=n/sum(n)
t<-data.frame(x,y,r,n,size)
run(t)

编辑 1:这段代码:

makeModel<-function(formula,weights,t) {
print(class(weights))
m <- lm(formula, na.action = na.omit, weights = weights,data=t)
return(m);
}
run<-function(t) {
f<-formula(y~x+r)
f <- as.formula("t$y~t$x+t$r")
m <- lm(y~x+r, na.action = na.omit, weights = t$size,data=t)
m <- lm(f, na.action = na.omit, weights = t$size,data=t)
m <- makeModel(f,t$size,t)
}

产生:

model.frame.default 错误(公式 = 公式,数据 = t,权重 = 权重,: 变量“(权重)”的无效类型(闭包)

编辑 2:作品:

makeModel <- function(formula, data) {
# size is looked in data first, which is why this works
m <- lm(formula, na.action = na.omit, weights = size, data = data) # works
#m <- lm(formula, na.action = na.omit, weights = data$size, data = data) # fails!
return(m)
}

r 很奇怪!

有谁知道为什么 weights=data$size 这一行失败了?

编辑 3:得到:weights=data$size 可以工作。

makeModel<-function(formula,w,data) {
print(class(weights))
m <- lm(formula, na.action = na.omit, weights = size, data = data) # works
m <- lm(formula, na.action = na.omit, weights = data$size, data = data) #works
m <- lm(formula, na.action = na.omit, weights = w,data=data) # fails
return(m);
}
run<-function(data) {
f<-formula(y~x+r)
#f <- as.formula("t$y~t$x+t$r")
m <- lm(y~x+r, na.action = na.omit, weights = data$size,data=data)
m <- lm(f, na.action = na.omit, weights = data$size,data=data)
m <- makeModel(f,data$size,data)
}

最后一个失败并显示: Error in eval(extras, data, env) : object 'w' not found

最佳答案

请参阅 ?as.formula 中的示例。您不应该从变量名中显式调用变量。公式应该是一个抽象的,lm 将知道要从 data 中提取哪些变量,您应该指定这些变量。

makeModels <- function(formula, data) {
# size is looked in data first, which is why this works
m <- lm(formula, na.action = na.omit, weights = size, data = data)
return(m)
}

run <- function(t) {
f <- formula(y ~ x + r)
m1 <- lm(formula = f, na.action = na.omit, weights = size, data = t)
m2 <- makeModels(formula = f, data = t)
return(list(m1, m2))
}

l<-20
x<-seq(0,1,1/l)
y<-sqrt(x)
r=round(runif(n = length(x), min = 0, max = 0.8))
n<-1:(l+1)
size=n/sum(n)
t<-data.frame(x,y,r,n,size)
run(t)

[[1]]

Call:
lm(formula = f, data = t, weights = t$size, na.action = na.omit)

Coefficients:
(Intercept) x r
0.327154 0.706553 -0.008167


[[2]]

Call:
lm(formula = formula, data = data, weights = size, na.action = na.omit)

Coefficients:
(Intercept) x r
0.327154 0.706553 -0.008167

关于r - 如何将公式作为参数传递给 r 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52576090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com