can you duplicate this in your other software ?
set.seed(42)
rv1 <- sample.int(3,1000,replace=TRUE)
rv2 <- sample.int(6,1000,replace=TRUE)
X1 <- 1:1000
X2 <- factor(c(rep("blue",500),rep("red",500)))
Y <- ifelse(X2=="blue",X1*rv1,
X1*rv2
)
(mydata <- data.frame(Y,
X1,
X2))
(my_lm <- lm(Y~X1*X2,data=mydata))
mydata$lm_pred <- predict(my_lm,newdata = mydata)
manual_pred <- function(a,b){
int <- my_lm$coefficients[[1]]
x1_coeff <- my_lm$coefficients[[2]]
x2red_coeff <- my_lm$coefficients[[3]]
x1_adjust_for_x2red <- my_lm$coefficients[[4]]
int +
a* ifelse(b=="red",x1_coeff+x1_adjust_for_x2red,x1_coeff) +
ifelse(b=="red",x2red_coeff,0)
}
mydata$lm_manpred <- manual_pred(mydata$X1,mydata$X2)
ggplot(data=mydata,
mapping=aes(x=X1,color=X2,
y=Y)) + geom_point() +
geom_line(aes(y=lm_pred),color="black",linetype=3,size=2) +
geom_line(aes(y=lm_manpred),color="red",linetype=2,size=1)