How to create two indicator variables using two specific observations in a dataset in RStudio

Hello. If anyone could please tell me how to create two indicator variables from two specific observations in a dataset, I'd be really grateful. I will need the two indicator variables later to produce multiple linear regression model.

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. Also, please see the homework policy.

Let's use the built-in mtcars dataset to show how this is done. We are going to see how mpg, as a response variable is affected by two other variables and the independent variables. The pieces are the data, mtcars, the mpg (miles per gallon) wt (vehicle weight) and drat (rear axle ratio) and the function lm and the values (result) object to capture the calculation.

fit <- lm(mpg ~ wt + drat, data = mtcars)
summary(fit)
#> 
#> Call:
#> lm(formula = mpg ~ wt + drat, data = mtcars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -5.4159 -2.0452  0.0136  1.7704  6.7466 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)   30.290      7.318   4.139 0.000274 ***
#> wt            -4.783      0.797  -6.001 1.59e-06 ***
#> drat           1.442      1.459   0.989 0.330854    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.047 on 29 degrees of freedom
#> Multiple R-squared:  0.7609, Adjusted R-squared:  0.7444 
#> F-statistic: 46.14 on 2 and 29 DF,  p-value: 9.761e-10

Created on 2020-04-06 by the reprex package (v0.3.0)

The choice of mpg on the left hand side of the \sim and wt and drat on the right hand is based on the design of the analysis. There's no right or wrong answer.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.