Advertisement
Guest User

Untitled

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