how to draw regression surface with one continuous predictor and one factor predictor in r?

86 Views Asked by At

Trying to do some regression on one continuous predictor and one factor predictor.

The code below works when using abline (but gets a: Warning message: In abline(models[[i]], col = labelColors[i], lty = 2:4) : only using the first two of 3 regression coefficients)), and draws a bunch of lines for the logit and loglog models.

How can I draw this surface or fake it somehow?

f<-function() {
    set.seed(123)
    colors<-c("red","green")
    labelColors<-c("black","cyan","magenta")
    x<-runif(100,0,1)
    f<-round(runif(100,0,.75))
    noise<-rnorm(length(x),0,.5)
    y<-(1+3*x+f/2+noise)/6
    plot(x,y,xlab="",ylab="")
    title("test")
    plot(x[f==0],y[f==0],ylim=c(0,1),col=colors[1])
    points(x[f==1],y[f==1], ylim=c(0.,1.),col=colors[2])
    title("test",sub="factor")
    m<-lm(y~x+factor(f),na.action=na.omit)
    logit<-betareg(y~x+factor(f),na.action=na.omit)
    loglog<-betareg(y~x+factor(f),na.action=na.omit,link="loglog")
    models<-list(m,logit,loglog)
    for(i in 1:length(models)) {  
        if(is(models[[i]],"lm")) {
            if(F) lines(x,predict(models[[i]]),col=labelColors[i],lty=2:4)
            else abline(models[[i]],col=labelColors[i],lty=2:4)
        }
        else lines(x,predict(models[[i]]),col=labelColors[i],lty=2:4)
    }
    for(i in 1:length(models))
        print(models[[i]])
    }
f()
0

There are 0 best solutions below