简体   繁体   English

R:如何在 glm 或 lm 中传递对变量的引用?

[英]R: how to pass in a reference to the variable in glm or lm?

so let's say i have a named vector:所以假设我有一个命名向量:

sorted = c(1,2,3)
names(sorted) = c("A","B","C")

and it'll look like following:它将如下所示:

> sorted
A    B    C
1    2    3

so this is a vector named A,B,C, and has value 1,2,3 respectively.所以这是一个名为 A、B、C 的向量,其值分别为 1、2、3。

and i also have a sample data:我还有一个样本数据:

data.ex = as.data.frame(matrix(rep(c(1,2,3,4),3), nrow = 3, ncol = 3))
colnames(data.ex) = c("A","B","C")

so this data frame has 3 columns named A,B,C as well.所以这个数据框也有 3 列名为 A、B、C。

I want to only predict C using value in A with glm():我只想使用 glm() 使用 A 中的值来预测 C:

fit.ex = glm(formula = C ~ names(sorted)[2],
         data = data.ex,
         family = binomial(link = "logit"))

but then, i'll keep getting the following error message:但是,我会不断收到以下错误消息:

Error in model.frame.default(formula = C ~ names(sorted)[2], data = data.ex,: 
variable lengths differ (found for 'names(sorted)[2]')

i read this article here and found the as.name() function, but still not working: http://www.ats.ucla.edu/stat/r/pages/looping_strings.htm我在这里阅读了这篇文章并找到了 as.name() 函数,但仍然无法正常工作: http : //www.ats.ucla.edu/stat/r/pages/looping_strings.htm

and i cannot find anything else thats similar to my problem.我找不到与我的问题类似的任何其他内容。 please, if there is another thread addressing this problem, guide me to it!请,如果有另一个线程解决这个问题,请指导我! or any kind of help is greatly appreciated!或任何形式的帮助,不胜感激! :) :)

Providing an answer based on the comments:根据评论提供答案:

sorted = c(A=1,B=2,C=3)
names(sorted) = c("A","B","C")
data.ex = data.frame(A=1:4,B=2:5,C=c(1,0,0,1))

Construct a list of formulas:构造一个公式列表:

forms <- lapply(names(sorted)[1:2],reformulate,response="C")
models <- lapply(forms,glm,data = data.ex,
                 family = binomial(link = "logit"))

Then you can do things like然后你可以做这样的事情

t(sapply(models,coef))

The plyr package is also handy for this sort of thing (eg plyr::ldply(models,coef) ) plyr包对于这类事情也很方便(例如plyr::ldply(models,coef)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM