Using R's lm on a dataframe with a list of predictors

前端 未结 3 651
臣服心动
臣服心动 2020-12-15 21:26

I have a dataframe with let\'s say N+2 columns. The first is just dates (mainly used for plotting later on), the second is a variable whose response to the remaining N colu

3条回答
  •  天命终不由人
    2020-12-15 22:05

    Using the formula notation y ~ . specifies that you want to regress y on all of the other variables in the dataset.

    df = data.frame(y = 1:10, x1 = runif(10), x2 = rnorm(10))
    # fits a model using x1 and x2
    fit <- lm(y ~ ., data = df) 
    # Removes the column containing x1 so regression on x2 only
    fit <- lm(y ~ ., data = df[, -2]) 
    

提交回复
热议问题