Najlepszy sposób na wykreślenie efektów interakcji z modelu liniowego
Aby pomóc w wypełnieniu tagu R tutaj, zamieszczam kilka pytań, które często otrzymywałem od uczniów. Przez lata opracowywałem własne odpowiedzi na te pytania, ale być może są lepsze sposoby, o których Nie wiem.
Pytanie: właśnie przeprowadziłem regresję z ciągłym y
i x
ale czynnikiem f
(Gdzie levels(f)
daje c("level1","level2")
)
thelm <- lm(y~x*f,data=thedata)
Teraz chciałbym wykreślić przewidywane wartości y
przez x
w podziale na grupy zdefiniowane przez f
. Wszystkie wątki, które dostaję są brzydkie i pokazują zbyt wiele linii.
Moja odpowiedź: Wypróbuj funkcję predict()
.
##restrict prediction to the valid data
##from the model by using thelm$model rather than thedata
thedata$yhat <- predict(thelm,
newdata=expand.grid(x=range(thelm$model$x),
f=levels(thelm$model$f)))
plot(yhat~x,data=thethedata,subset=f=="level1")
lines(yhat~x,data=thedata,subset=f=="level2")
Czy istnieją inne pomysły, które są (1) łatwiejsze do zrozumienia dla nowicjusza i/lub (2) lepsze z innej perspektywy?
4 answers
Pakiet efektów ma dobre metody wykreślania przewidywanych wartości regresji.
thedata<-data.frame(x=rnorm(20),f=rep(c("level1","level2"),10))
thedata$y<-rnorm(20,,3)+thedata$x*(as.numeric(thedata$f)-1)
library(effects)
model.lm <- lm(formula=y ~ x*f,data=thedata)
plot(effect(term="x:f",mod=model.lm,default.levels=20),multiline=TRUE)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-08 17:26:04
Wciąż próbuję ogarnąć mój mózg. Dla porównania, tak bym to zrobił (używając ggplot2):
thedata <- data.frame(predict(thelm), thelm$model$x, thelm$model$f)
ggplot(thedata, aes(x = x, y = yhat, group = f, color = f)) + geom_line()
Logika ggplot() jest dość intuicyjna, myślę, że - grupowanie i kolorowanie linii przez f. wraz z rosnącą liczbą grup, nie konieczność określania warstwy dla każdej z nich jest coraz bardziej pomocna.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-08 17:31:32
Nie jestem ekspertem w R. ale używam:
xyplot(y ~ x, groups= f, data= Dat, type= c('p','r'),
grid= T, lwd= 3, auto.key= T,)
Jest to również opcja:
interaction.plot(f,x,y, type="b", col=c(1:3),
leg.bty="0", leg.bg="beige", lwd=1, pch=c(18,24),
xlab="",
ylab="",
trace.label="",
main="Interaction Plot")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-22 22:41:22
Oto mała zmiana w doskonałej sugestii Matta i rozwiązanie podobne do Helgiego, ale z ggplot. Jedyną różnicą od powyższego jest to, że użyłem geom_smooth (method = 'lm), który wykreśla linie regresji bezpośrednio.
set.seed(1)
y = runif(100,1,10)
x = runif(100,1,10)
f = rep(c('level 1','level 2'),50)
thedata = data.frame(x,y,f)
library(ggplot2)
ggplot(thedata,aes(x=x,y=y,color=f))+geom_smooth(method='lm',se=F)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-22 14:46:09