[英]Pass df column names to nested equation in Graph Printing Function
I need some clarification on the primary post on Passing a data.frame column name to a function 我需要在有关将data.frame列名传递给函数的主要文章中进行一些说明
I need to create a function that will take a testSet, trainSet, and colName(aka predictor) as inputs to a function that prints a plot of the dataset with a GAM model trend line. 我需要创建一个函数,该函数将使用testSet,trainSet和colName(aka预测变量)作为函数的输入,该函数使用GAM模型趋势线打印数据集的图。
The issue I run into is: 我遇到的问题是:
plot.model = function(predictor, train, test) {
mod = gam(Response ~ s(train[[predictor]], spar = 1), data = train)
...
}
#Function Call
plot.model("Predictor1", 1.0, crime.train, crime.test)
I can't simply pass the predictor as a string into the gam function, but I also can't use a string to index the data frame values as shown in the link above. 我不能简单地将预测变量作为字符串传递给gam函数,但也不能使用字符串来索引数据帧值,如上面的链接所示。 Somehow, I need to pass the colName key to the game function. 不知何故,我需要将colName键传递给游戏功能。 This issue occurs in other similar scenarios regarding plotting. 在与绘图有关的其他类似情况下也会发生此问题。
plot <- ggplot(data = test, mapping = aes(x=predictor, y=ViolentCrimesPerPop))
Again, I can't pass a string value for the column name and I can't pass the column values either. 同样,我也不能传递列名的字符串值,也不能传递列值。
Does anyone have a generic solution for these situations. 有没有人有针对这些情况的通用解决方案。 I apologize if the answer is buried in the above link, but it's not clear to me if it is. 对于以上链接中隐藏的答案,我深表歉意,但我不清楚。
Note: A working gam function call looks like this: 注意:一个有效的gam函数调用如下所示:
mod = gam(Response ~ s(Predictor1, spar = 1.0), data = train)
Where the train set is a data frame with column names "Response" & "Predictor". 火车组是一个数据框,其列名称为“ Response”和“ Predictor”。
Use aes_string
instead of aes
when you pass a column name as string. 当您将列名作为字符串传递时,请使用aes_string
而不是aes
。
plot <- ggplot(data = test, mapping = aes_string(x=predictor, y=ViolentCrimesPerPop))
For gam
function:: Example which is copied from gam
function's documentation. 对于gam
函数::从gam
函数文档中复制的示例。 I have used vector, scalar is even easier. 我用过向量,标量甚至更容易。 Its just using paste
with a collapse
parameter. 它仅使用带有collapse
参数的paste
。
library(mgcv)
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=400,dist="normal",scale=2)
# String manipulate for formula
formula <- as.formula(paste("y~s(", paste(colnames(dat)[2:5], collapse = ")+s("), ")", sep =""))
b <- gam(formula, data=dat)
is same as 与...相同
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.