Repeaded measures for id, but id can never be in different subtypes, how to analyse difference between subtypes? In this example data I want to test if the variable "w" is different between subtypes. There are repeated measures for ids for each subtype, but the same id can never be found in different subtypes. How should it be analysed correctly? Does id have to be nested in subtype as a random factor, somehow? But can subtype also be a fixed factor? Highly appreciate guidance!
id <- c(1,1,1,2,3,4,5,5,5,6,7,8,8,8,9,9)
subtype <- c("A","A","A","C","B","A","C","C","C","B","A", "B", "B", "B", "A","A")
w<- sample(100,16)
df <- data.frame(id,subtype,w)
df
library(lme4)
library(car)
lm <- lmer(w~subtype+ (1|id), data=df)
Anova(lm, type=3)
summary(lm)
l <-emmeans(lm, "subtype")
pairs(l)


All the alternatives that you mention are possible (except for the nesting one, see below). It all depends on your data. If you have some domain knowledge (i.e. some insight into the nature of the data) then you can use it to choose the right model, otherwise, you just have to try all the models and then compare them using some model selection method (lme4 provides e.g. AIC, BIC, ... for each fitted model).
Here is some intuition as to how to use domain knowledge:
Subtype as fixed or random effects? If you know that the subtypes are completely dissimilar from each other, and the $w$ values of one subtype give you absolutely no indication as to what the $w$ values of the other subtypes might be, then you would use subtypes as fixed effects, thus each subtype gets its own independent intercept. But if you would say that the subtypes do have some similarity and the $w$ values of one subtype do give you some indication as to what the $w$ values of the other subtypes might be, you should use random effects. Random effects models usually lead to the effects being similar to each other (at least when using the defaults as in lme4).
Ids as fixed or random effects? Here, the same applies as to subtypes.
Nesting id in subtype? Nesting would be useful if an id in one subtype would lead to different $w$ values than the same id in another subtype. But since each of your ids is always only appearing in a unique subtype, nesting is not appropriate.
In your example, subtype is a fixed effect, and id a random effect. Presuming that $w$-values for the same id are similar and that subtypes are also similar, I would suggest modelling both as random effects:
lm <- lmer(w ~ -1 + (1|subtype) + (1:id), data = df)

But, as mentioned above, trying alternative models and applying some model selection might be useful.
