I am relatively new to R. I want to create a function in which arguments will be variables (objects) in the function glm called within my function.
I have a dataframe containing multiple variables (columns). I wish to run multiple logistic regressions using glm() using the same predictors (terms) with only one changing, hence the need for the function. I would like to be able to specify the name of this predictor and of the object created by glm().
For example:
myfunc <-function (myvar, mymodel) {
mymodel <- glm (var1 ~ var2 var3 var4 myvar, data = myframe, family = "binomial")
}
I would like the arguments of my function to allow me to run the same the analysis multiple times by replacing one variable and to obtain results in different objects. For example,
myfunc(var_A, model_A)
should be equivalent to
model_A<- glm (var1 ~ var2 var3 var4 var_A, data = myframe, family = "binomial")
and
myfunc(var_B, model_B)
should be equivalent to
model_B<- glm (var1 ~ var2 var3 var4 var_B, data = myframe, family = "binomial")
And so on.
I cannot find how to write my function so that the name of the arguments become objects within my function.
Can someone help me please ?
Many thanks in advance
CodePudding user response:
You can try using the lapply function For example, see the below code
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
I will try to generate list of linear models with Sepal.Length as the Independent Variable, Sepal.Width as the common dependent variable across models and one more variable each time from the set ("Petal.Length", "Petal.Width", "Species")
changing.variables <- c("Petal.Length", "Petal.Width", "Species")
df <- iris[changing.variables]
list.lm <- lapply(df, FUN = function(x) lm(Sepal.Length~Sepal.Width x, data = iris))
list.lm
Output
> list.lm
$Petal.Length
Call:
lm(formula = Sepal.Length ~ Sepal.Width x, data = iris)
Coefficients:
(Intercept) Sepal.Width x
2.2491 0.5955 0.4719
$Petal.Width
Call:
lm(formula = Sepal.Length ~ Sepal.Width x, data = iris)
Coefficients:
(Intercept) Sepal.Width x
3.4573 0.3991 0.9721
$Species
Call:
lm(formula = Sepal.Length ~ Sepal.Width x, data = iris)
Coefficients:
(Intercept) Sepal.Width xversicolor xvirginica
2.2514 0.8036 1.4587 1.9468
CodePudding user response:
Another possible solution is :
data(iris)
iris <- iris[1:100,]
myfunc <-function (myvar, mymodel) {
formula <- reformulate(termlabels = c('Sepal.Length',myvar), response = 'Species')
model <- glm (formula, data = iris, family = "binomial")
assign(mymodel,model,pos = globalenv())
}
myfunc("Sepal.Width","model_a")
here the inputs of the function must be characters. With your example the function should look like this :
myfunc <-function (myvar, mymodel) {
formula <- reformulate(termlabels = c("var2" , "var3" , "var4" , myvar), response = 'var1')
model <- glm (formula, data = myframe, family = "binomial")
assign(mymodel,model,pos = globalenv())
}