Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -----------------------------------------------------------------
- # 1.1 - Modelo Linear
- # -----------------------------------------------------------------
- # Este exemplo mostra como estimar um modelo u = a + bx + e
- # e interpretar os resultados:
- # - Plotar os dados na forma: line e scatter plot.
- # - Teste de significância de 'a' e 'b'
- # - R2 capacidade de previsão do modelo
- # - Calcular erro manualmente e usando a rotina do R
- # - Comportamento dos resíduos: normalidade e gráfico
- # - Plotar reta ajustada com valores previstos (fitted values)
- # Carrega arquivo excel
- install.packages("readxl")
- install.packages("tibble")
- library(readxl)
- library(tibble)
- File <- "C:/Users/AlunoSA/Downloads/Ch01-Regressoes.xlsx"
- df <- read_excel(File, col_names=TRUE, sheet='Plan1')
- df
- par(mar=c(3,2.5,2,1),mgp=c(1.5,0.5,0.2))
- plot(df$x,df$y,pch=16,col="black",main="Scatter Plot",
- xlim=c(0,60), ylim=c(0,60))
- mod <- lm(df$y~df$x) # y= b1 + b2X
- summary(mod)
- abline(mod,lwd=2,col="red")
- yhat <- mod$fitted.values
- lines(df$x,yhat,type="p",pch=16,col="red")
- segments(df$x,df$y,df$x,yhat, lty=2)
- b1 <- round(mod$coefficients[1],3)
- b2 <- round(mod$coefficients[2],3)
- eq <- paste("Y =",b1,"+",b2,"X")
- text(20,45,eq,col="red",cex=0.8,lty=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement