Home > Enterprise >  r run hardcoded regression on subset of data
r run hardcoded regression on subset of data

Time:04-06

If this is my dataset:

dataset <- data.frame(
           ID = 1:6,
           Group = c("Red", "Red", "Blue", "Red", "Blue", "Blue"),
           X = c(10, 11, 11, 12, 9, 13))
ID Group X
1 Red 10
2 Red 11
3 Blue 11
4 Red 12
5 Blue 9
6 Blue 13

I have two linear regression equations:

 (Eq. 1) Y ~ 34      0.35 * X [ where Group == "Red" ]
 (Eq. 2) Y ~ 33.67   0.37 * X [ where Group == "Blue"]

How do I predict Y from this regression question using my dataset?

CodePudding user response:

Tidyverse solution would be

library(tidyverse)
dataset %>% mutate(Y = ifelse(Group == "Red", 34      0.35 * X, 33.67   0.37 * X ))

This assumes that you only have Group names of red and blue.

  • Related