I have a dataset which includes some nested variables. For example, I have the following variables:
the speed
of a car, the existence of another car following it other_car
and, if there is another car, the distance between the two cars distance
. Dummy dataset:
speed <- c(30,50,60,30,33,54,65,33,33,54,65,34,45,32)
other_car <- c(0,1,0,0,0,1,1,1,1,0,1,0,1,0)
distance <- c(NA,20,NA,NA,NA,21,5,15,17,NA,34,NA,13,NA)
dft <- data.frame(speed,other_car,distance)
I would like to include the variables other_car
and distance
in a model with the form of nested variables, i.e. if the car is present consider also the distance. Following an approach mentioned here: https://stats.stackexchange.com/questions/372257/how-do-you-deal-with-nested-variables-in-a-regression-model , I tried the following:
dft <- data.frame(speed,other_car,distance)
dft$other_car<-factor(dft$other_car)
lm_speed <- lm(speed ~ dft$other_car + dft$other_car:dft$distance)
summary(lm_speed)
Which gives the following error:
Error in
contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
Any ideas?