#ver librerias
library("datasets")
# modelo lineal
dataframe1 = Orange
dataframe1
library(readr)
write_csv( dataframe1 , "./salidas/df_Orange.csv")
attach(Orange)
The following objects are masked from Orange (pos = 3):
age, circumference, Tree
The following objects are masked from Orange (pos = 4):
age, circumference, Tree
#el primer paso para verificar si se debe implementar
#modelo lineal es mediante el grafico
plot(Orange)
#existe una relacion lineal entre
#age y circumference
plot(age,circumference)
#siguiente para validar si implemento o no el modelo
# es evaluar el cor solo se acepta de 1-0.6
cor.test(circumference,age)
Pearson's product-moment correlation
data: circumference and age
t = 12.9, df = 33, p-value = 1.931e-14
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.8342364 0.9557955
sample estimates:
cor
0.9135189
#creo modelo lineal
modelo1=lm(circumference ~ age)
modelo1
Call:
lm(formula = circumference ~ age)
Coefficients:
(Intercept) age
17.3997 0.1068
#ver sus coeficiente circumference=age*a+b
modelo1
Call:
lm(formula = circumference ~ age)
Coefficients:
(Intercept) age
17.3997 0.1068
#analizar modelo p-value<0.05 y R2>0.75
summary(modelo1)
Call:
lm(formula = circumference ~ age)
Residuals:
Min 1Q Median 3Q Max
-46.310 -14.946 -0.076 19.697 45.111
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.399650 8.622660 2.018 0.0518 .
age 0.106770 0.008277 12.900 1.93e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 23.74 on 33 degrees of freedom
Multiple R-squared: 0.8345, Adjusted R-squared: 0.8295
F-statistic: 166.4 on 1 and 33 DF, p-value: 1.931e-14
circumference=agea+b circumference = age 0.106770 + 17.399650
# circumference=age*0.1068+17.3997
#arbol de 700 años
circumference_2 = 700*0.1068+17.3997
circumference_2
[1] 92.1597
#arbol de 800 años
800*0.1068+17.3997
[1] 102.8397
#grafico
plot(age,circumference,pch=19)
abline(modelo1,col="red")
#predecir para arboldes de 950 a 1000
predecir=predict(modelo1,list(age=c(950:1000)))
predecir
1 2 3 4 5 6 7 8 9
118.8315 118.9382 119.0450 119.1518 119.2585 119.3653 119.4721 119.5789 119.6856
10 11 12 13 14 15 16 17 18
119.7924 119.8992 120.0059 120.1127 120.2195 120.3262 120.4330 120.5398 120.6466
19 20 21 22 23 24 25 26 27
120.7533 120.8601 120.9669 121.0736 121.1804 121.2872 121.3939 121.5007 121.6075
28 29 30 31 32 33 34 35 36
121.7143 121.8210 121.9278 122.0346 122.1413 122.2481 122.3549 122.4617 122.5684
37 38 39 40 41 42 43 44 45
122.6752 122.7820 122.8887 122.9955 123.1023 123.2090 123.3158 123.4226 123.5294
46 47 48 49 50 51
123.6361 123.7429 123.8497 123.9564 124.0632 124.1700
#ver por años los resultados
(matriz=data.frame(c(950:1000),predecir))