Reputation: 907
I am creating a function that will run all my analysis, as a method of streamlining 600 lines of code to avoid errors. I am having trouble with the line of the GLM. I have some analysis where the dataframe is filtered, however when I vectors DV and ME they are from a dataframe that already exist (my_data$Hire
, my_data$Con
). I need to pass these values in such a way that R will recognise that they are the correct DV and ME from the newly created dataframe - GM.
calculatemodels <- function(type, DV, ME, ME2, MD, df, test, MA_data, model, number, samplename){
if (type == "A1"){
GM <- my_data %>% filter(Gender == 1)
m <- glm(DV ~ ME, data=GM) # problem here, need to pass DV and ME vectors correctly.
MA_data <- CollectDEffect(test, m, MA_data, namestring(ME), model, number, samplename)
}
return(MA_data)
}
MA_data <- calculatemodels("A1", my_data$Hire, my_data$Con, , , my_data,
"", MA_data, "", "1", "full")
I tried using get and paste, however it does not work. In a nutshell, I need to pass the name of the DV and ME and have the function recognize that these are the vectors for the model, not pass vectors that are already attached to a dataframe, i.e., my_data$Hire
Upvotes: 0
Views: 381
Reputation: 4233
As far as I understand, you are trying to pass ingredients of the model as vectors and run the glm
model on them.
There are a couple of issues here. First of all, my_data
is not the function variable. I guess it exists in your global environment. Second, you need to create a formula (as.formula
) from column names and use it as input to the model:
calculatemodels <- function(type, DV, ME, ME2, MD,
df, test, MA_data,
model, number,samplename){
if (type == "A1"){
GM <- my_data %>% filter(Gender == 1)
glm_model <- as.formula(paste0(DV,"~",ME))
m <- glm(glm_model, data=GM) # problem here, need to pass DV and ME vectors correctly.
MA_data <- CollectDEffect(test, m, MA_data, namestring(ME), model, number, samplename)
}
return(MA_data)
}
You need to use column names (Hire
and Con
) as input values to the function:
MA_data <- calculatemodels("A1", "Hire", "Con", , , my_data,
"", MA_data, "", "1", "full")
Hope this resolves the issue.
Upvotes: 1