0

I was having trouble with plotting lines with my non-linear regression because the lengths of the plotting variables were different because the regression was omitting some observations. I fixed that using na.action=na.exclude. Now the "lengths differ" warning doesn't show anymore, but the lines() simply doesn't show my non-linear prediction.

I'm sorry if this is kind of vague, but I really don't know what is happening because there is no error message.

Here's the code I'm using:

data_lm <- read.csv('notaport.csv', sep=';', dec=',')

regs <- lm(notaport ~ tamturma, data=data_lm)
regm <- lm(notaport ~ tamturma + I(tamturma**2), data=data_lm, na.action = na.exclude)

plot(data_lm$tamturma, data_lm$notaport, xlab='Tamanho da Turma', ylab = 'Notas de Português', pch='.')
abline(regs, col='red')

linearpred_regm <- predict(regm) 

plot(data_lm$tamturma, data_lm$notaport, xlab='Tamanho da Turma', ylab = 'Notas de Português', pch='.')
lines(linearpred_regm, data_lm$tamturma, col='red')

summary(data_lm)
summary(regs)
summary(regm)

My data can be downloaded with this link in We Transfer.

Phil
  • 7,287
  • 3
  • 36
  • 66
luka1156
  • 181
  • 1
  • 8

2 Answers2

0

You are passing the y axis variables to the x axis and vice-versa. Also, you need to make sure that your x variables are ordered correctly:

plot(data_lm$tamturma, data_lm$notaport, xlab='Tamanho da Turma', ylab= 'Notas de Português', pch='.')

ord <- order(data_lm$tamturma)

lines(data_lm$tamturma[ord], linearpred_regm[ord], col='red')

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
0

Sometimes the curve needs more smoothing. Perhaps not in this case, but other times it might. Then you can use this method:

newdata <- data.frame(tamturma=seq(6,65,0.1))
linearpred_regm <- predict(regm, newdata=newdata) 
lines(newdata$tamturma, linearpred_regm, col='red')
Edward
  • 10,360
  • 2
  • 11
  • 26