Analisis del D.F mtcars. Agarre solo los autos con +200 HP para ver cuanto consumia cada uno por milla en Galones.
powercars <- mtcars[which(mtcars$hp >= 200),]
print(powercars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
## Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4
## Ford Pantera L 15.8 8 351 264 4.22 3.170 14.50 0 1 5 4
## Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
par(mfrow = c(1,1))
fit <- lm(powercars$mpg ~ powercars$hp)
plot(powercars$hp, powercars$mpg, main = "+200 hp cars miles per gallon",
xlab = "Horsepower", ylab = "Millas por Galon", bg = "red", pch = 21)
abline(coef(fit))
summary(fit)
##
## Call:
## lm(formula = powercars$mpg ~ powercars$hp)
##
## Residuals:
## 1 2 3 4 5 6 7
## 1.002975 -1.528989 -1.870998 1.915988 0.002975 1.853157 -1.375107
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.91780 4.24989 1.157 0.2995
## powercars$hp 0.03420 0.01689 2.025 0.0988 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.78 on 5 degrees of freedom
## Multiple R-squared: 0.4505, Adjusted R-squared: 0.3406
## F-statistic: 4.1 on 1 and 5 DF, p-value: 0.09877
text(powercars$hp, powercars$mpg, labels= row.names(powercars), cex= 0.7, pos = 4)
fit2 <- lm(mpg ~ hp, powercars)
(newdata=data.frame(hp=600))
## hp
## 1 600
prediction <- predict(fit2,newdata,interval="predict")
c(print("consumo estimado 600hp en Miles per gallon"), prediction[,1])
## [1] "consumo estimado 600hp en Miles per gallon"
## [1] "consumo estimado 600hp en Miles per gallon"
## [2] "25.4383488150683"
pp <- layout(matrix(c(1,2,3,3), 2, 2, byrow=T))
fit <- lm(powercars$mpg ~ powercars$hp)
plot(powercars$hp, powercars$mpg, main = "+200 hp cars miles per gallon",
xlab = "Horsepower", ylab = "Millas por Galon", bg = "red", pch = 21)
abline(coef(fit))
summary(fit)
##
## Call:
## lm(formula = powercars$mpg ~ powercars$hp)
##
## Residuals:
## 1 2 3 4 5 6 7
## 1.002975 -1.528989 -1.870998 1.915988 0.002975 1.853157 -1.375107
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.91780 4.24989 1.157 0.2995
## powercars$hp 0.03420 0.01689 2.025 0.0988 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.78 on 5 degrees of freedom
## Multiple R-squared: 0.4505, Adjusted R-squared: 0.3406
## F-statistic: 4.1 on 1 and 5 DF, p-value: 0.09877
text(powercars$hp, powercars$mpg, labels= row.names(powercars), cex= 0.7, pos = 2)
nopower <- mtcars[which(mtcars$hp <= 100),]
fitdos <- lm(nopower$mpg ~ nopower$hp)
plot(nopower$hp, nopower$mpg, main = "-100hp MPG", xlab = "Horsepower", ylab = "MPG",
bg = "blue", pch = 21)
abline(coef(fitdos))
text(nopower$hp, nopower$mpg, labels= row.names(nopower), cex= 0.7, pos = 2)
fittres <- lm(mtcars$mpg ~ mtcars$hp)
plot(mtcars$hp, mtcars$mpg, main = "Todos los autos grafico MPG ~ HP", bg = "yellow",
xlab = "HP", ylab = "MPG", pch = 21)
text(mtcars$hp, mtcars$mpg, labels= row.names(mtcars), cex= 0.7, pos = 2)
abline(coef(fittres))
axis(side = 1, at = seq(0, 340, by = 25))
fittres <- lm(mtcars$mpg ~ mtcars$hp)
plot(mtcars$hp, mtcars$mpg, main = "Todos los autos grafico MPG ~ HP", bg = "yellow",
xlab = "HP", ylab = "MPG", pch = 21)
text(mtcars$hp, mtcars$mpg, labels= row.names(mtcars), cex= 0.8, pos = 3)
abline(coef(fittres))
axis(side = 1, at = seq(0, 340, by = 25))
Aunque si se mira solo los autos de +200 hp pasa lo contrario
Asi te das cuenta que mirando una pequena parte de la data puede ser confuso.
orga <- layout(matrix(c(1,2,3,3), 2, 2, byrow=T))
hist(mtcars$cyl, col = "red", border = "black", main = "Autos y cantidad Cilindros",
xlab = "Nro Cilindros", ylab = "Nro Autos")
hist(mtcars$carb, col = "blue", border = "black", main = "Autos y cantidad Carburadores",
xlab = "Nro carburadores", ylab = "nro autos")
axis(side = 2, at = c(0,5,10,15,17))
lineauno <- lm(mtcars$carb ~ mtcars$cyl)
plot(mtcars$cyl, mtcars$carb, main = "Relacion Carburadores & Cilindros", xlab = "cilindros",
ylab = "Carburadores", pch = 21, bg = "pink")
abline(coef(lineauno))