I have the following data frame:
set.seed(1)
x<-sample(1:30, 50, T)
y<-sample(90:450, 50, T)
df<-data.frame(x,y)
}
And I want to create a function in which the linear model is developed only by specifying the data frame. As seen below:
regresion<-function(x){
reg<-lm(x[,1]~x[,2], data = x)
return(summary(reg))
But obviously, the argument x
I declared is a vector and not a data frame.
How should I do to indicate that the argument x
is a data frame.
Thanks in advance
You could manipulate the regression formula instead of building it by passing the data as vectors. Something like that:
In this way you get an OLS model for a data.frame in which the first column is dependent and all other independent.