How to add all variables its second degree in lm()? [duplicate] - r

This question already has an answer here:
R:fit dynamic number of explanatory variable into polynomial regression
(1 answer)
Closed 6 years ago.
I have a dataframe with 16 variables. When I do multiple linear regression I do the following:
fit <- lm(y ~ .,data=data)
Now, I know how to add a second degree term of one of the variables:
fit2 <- lm(y ~ poly(x1,2) + .,data=data)
But now I don't want to write this out for all of my 16 variables. How can I do this in an easy way for all my variables?

When assuming the first variable in data is our 'y', we get this:
as.formula(
paste('y ~',paste('poly(',colnames(data[-1]),',2)',collapse = ' + ')
)

Related

Problem in writing program to compute linear model for undefined numbers of independent variables [duplicate]

This question already has answers here:
How to succinctly write a formula with many variables from a data frame?
(6 answers)
Closed 1 year ago.
if just for two independent variables, we have
X1=rnorm(n=3, mean=4, sd=2)
X2=rnorm(n=3, mean=2, sd=1)
e=rnorm(n=3, mean=0, sd=1)
Y=5+2*X1+3*X2+e
M=data.frame(Y, X1, X2)
My_lm= lm(Y~X1+X2, data=M)
The issue is writing the program that can compute any number of X's and not a fixed number of independent variables.
Just use the . symbol within your formula and this will take care of all the independent variables:
eg
My_lm= lm(Y ~ ., data = M)
You can use reformulate to construct the formula dynamically.
response <- 'Y'
My_lm <- lm(reformulate(setdiff(names(M), response), response), data = M)
Here reformulate returns :
reformulate(setdiff(names(M), response), response)
#Y ~ X1 + X2

How to calculate all possible models for a given set of variables [duplicate]

This question already has answers here:
How to run lm models using all possible combinations of several variables and a factor
(2 answers)
Closed 5 years ago.
I would like to find a command in R, in order to calculate all possible models for a given set of variables.
For example, for three variables x1, x2 and x3 there are 8 possible models:
m1: x1+x2+x3
m2: x1*x2+x3
m3: x1*x3+x2
m4: x2*x3+x1
m5: x1*x2+x1*x3
m6: x1*x2+x2*x3
m7: x1*x3+x2*x3
m8: x1*x2*x3
If I have 5 variables there are 6894 different models (including all possible interaction) but I would like to confirm with R.
The answer is here
options(na.action = "na.fail") # avoid getting an error
library(MuMIn)
fullmodel <- lm(y ~ x1 * x2 * x3)
dredge(fullmodel, fixed = ~ x1 + x2 + x3)

is there lm(.~x,data)? how can i run many y in lm formula [duplicate]

This question already has an answer here:
Fitting a linear model with multiple LHS
(1 answer)
Closed 5 years ago.
I know about lm(y~. , data)
but I'm wondering about the opposite direction
If I want to put multiple y to lm function, how should I do that? Using for? loop?
My desired result is getting each p-value and beta value of Y. There is just one X.
lm supports a matrix on the LHS of the formula:
f <- as.formula(sprintf("cbind(%s) ~ Species",
paste(names(iris)[names(iris) != "Species"], collapse = ", ")))
#cbind(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width) ~ Species
summary(lm(f, data = iris))

Predict new data using new x values and polynomial regression in R [duplicate]

This question already has an answer here:
Using predict to find values of non-linear model
(1 answer)
Closed 6 years ago.
I need to find a high degree polynomial fit to a set of data, then use that relationship to predict y values given x values. Here is a simplified example of the premise of my problem. I must create a regression (we can just do 2nd degree here, but I need a technique that can handle polynomials of any degree), then predict new y values given new x values.
dfram <- data.frame('x'=c(1,2,3,4))
dfram$y <- c(1,4,9,16)
pred <- data.frame('x'=c(5,6))
# predict pred$y using n degree trend in dfram
Here is the skeleton:
dfram <- data.frame('x'=c(1,2,3,4))
dfram$y <- c(1,4,9,16)
pred <- data.frame('x'=c(5,6))
myFit <- lm(y ~ poly(x,2), data=dfram)
predict(myFit, pred)
1 2
25 36
You can change the degree of polynomial with poly() arguments.

Linear Regression in R with variable number of explanatory variables [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Specifying formula in R with glm without explicit declaration of each covariate
how to succinctly write a formula with many variables from a data frame?
I have a vector of Y values and a matrix of X values that I want to perform a multiple regression on (i.e. Y = X[column 1] + X[column 2] + ... X[column N])
The problem is that the number of columns in my matrix (N) is not prespecified. I know in R, to perform a linear regression you have to specify the equation:
fit = lm(Y~X[,1]+X[,2]+X[,3])
But how do I do this if I don't know how many columns are in my X matrix?
Thanks!
Three ways, in increasing level of flexibility.
Method 1
Run your regression using the formula notation:
fit <- lm( Y ~ . , data=dat )
Method 2
Put all your data in one data.frame, not two:
dat <- cbind(data.frame(Y=Y),as.data.frame(X))
Then run your regression using the formula notation:
fit <- lm( Y~. , data=dat )
Method 3
Another way is to build the formula yourself:
model1.form.text <- paste("Y ~",paste(xvars,collapse=" + "),collapse=" ")
model1.form <- as.formula( model1.form.text )
model1 <- lm( model1.form, data=dat )
In this example, xvars is a character vector containing the names of the variables you want to use.

Resources