Devon Luongo Devon Luongo - 1 year ago 60
R Question

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

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.

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. Somehow, I need to pass the colName key to the game function. 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:

mod = gam(Response ~ s(Predictor1, spar = 1.0), data = train)

Where the train set is a data frame with column names "Response" & "Predictor".

Answer Source

Use aes_string instead of aes when you pass a column name as string.

plot <- ggplot(data = test, mapping = aes_string(x=predictor, y=ViolentCrimesPerPop))

For gam function:: Example which is copied from gam function's documentation. I have used vector, scalar is even easier. Its just using paste with a collapse parameter.

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download