鈻讹笍 Tematyka

  • Model liniowy z 1 zmienn膮 obja艣niaj膮c膮
    • Zmienne
    • Wyniki
  • Model liniowy z 2 zmiennymi obja艣niaj膮cymi


#Wykorzystywany zbi贸r danych:
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
?lm
## uruchamianie serwera httpd dla pomocy ... wykonano

馃摌 Model liniowy z 1 zmienn膮 obja艣niaj膮c膮

Zmienna obja艣niana: spalanie paliwa, zmienna obja艣niaj膮ca: waga samochodu.

model1<-lm(mpg~wt,data=mtcars)

#Podstawowe informacje.
model1
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Coefficients:
## (Intercept)           wt  
##      37.285       -5.344
#Rozszerzony widok informacji o modelu.
summary(model1)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

Wy艣wietlenie wynik贸w

Wsp贸艂czynniki

model1$coefficients
## (Intercept)          wt 
##   37.285126   -5.344472

Wsp贸艂czynniki i inne informacje (dotycz膮ce testowania istotno艣ci parametr贸w)

summary(model1)$coefficients
##              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 37.285126   1.877627 19.857575 8.241799e-19
## wt          -5.344472   0.559101 -9.559044 1.293959e-10

Warto艣膰 wsp贸艂czynnika kierunkowego

summary(model1)$coefficients[2,1]
## [1] -5.344472
summary(model1)$coef[2,1]
## [1] -5.344472

Warto艣膰 wyrazu wolnego

summary(model1)$coefficients[1,1]
## [1] 37.28513

Wsp贸艂czynnik R-kwadrat

summary(model1)$r.squared
## [1] 0.7528328
Reszty
model1$residuals
##           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
##          -2.2826106          -0.9197704          -2.0859521           1.2973499 
##   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
##          -0.2001440          -0.6932545          -3.9053627           4.1637381 
##            Merc 230            Merc 280           Merc 280C          Merc 450SE 
##           2.3499593           0.2998560          -1.1001440           0.8668731 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
##          -0.0502472          -1.8830236           1.1733496           2.1032876 
##   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
##           5.9810744           6.8727113           1.7461954           6.4219792 
##       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
##          -2.6110037          -2.9725862          -3.7268663          -3.4623553 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
##           2.4643670           0.3564263           0.1520430           1.2010593 
##      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
##          -4.5431513          -2.7809399          -3.2053627          -1.0274952
summary(model1)$residuals
##           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
##          -2.2826106          -0.9197704          -2.0859521           1.2973499 
##   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
##          -0.2001440          -0.6932545          -3.9053627           4.1637381 
##            Merc 230            Merc 280           Merc 280C          Merc 450SE 
##           2.3499593           0.2998560          -1.1001440           0.8668731 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
##          -0.0502472          -1.8830236           1.1733496           2.1032876 
##   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
##           5.9810744           6.8727113           1.7461954           6.4219792 
##       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
##          -2.6110037          -2.9725862          -3.7268663          -3.4623553 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
##           2.4643670           0.3564263           0.1520430           1.2010593 
##      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
##          -4.5431513          -2.7809399          -3.2053627          -1.0274952
Warto艣ci teoretyczne
model1$fitted.values
##           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
##           23.282611           21.919770           24.885952           20.102650 
##   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
##           18.900144           18.793255           18.205363           20.236262 
##            Merc 230            Merc 280           Merc 280C          Merc 450SE 
##           20.450041           18.900144           18.900144           15.533127 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
##           17.350247           17.083024            9.226650            8.296712 
##   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
##            8.718926           25.527289           28.653805           27.478021 
##       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
##           24.111004           18.472586           18.926866           16.762355 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
##           16.735633           26.943574           25.847957           29.198941 
##      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
##           20.343151           22.480940           18.205363           22.427495
# Uwaga! Funkcja poni偶ej nie zwr贸ci wynik贸w.
# summary(model1)$fitted.values

plot(mtcars$mpg,mtcars$wt)

馃摌 Model liniowy z 2 zmiennymi obja艣niaj膮cymi

Zmienna zale偶na: spalanie, dwie zmienne niezale偶ne: waga samochodu i przespieszenie

model2<-lm(mpg~wt+qsec,data=mtcars)
model2
## 
## Call:
## lm(formula = mpg ~ wt + qsec, data = mtcars)
## 
## Coefficients:
## (Intercept)           wt         qsec  
##     19.7462      -5.0480       0.9292
summary(model2)
## 
## Call:
## lm(formula = mpg ~ wt + qsec, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.3962 -2.1431 -0.2129  1.4915  5.7486 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  19.7462     5.2521   3.760 0.000765 ***
## wt           -5.0480     0.4840 -10.430 2.52e-11 ***
## qsec          0.9292     0.2650   3.506 0.001500 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.596 on 29 degrees of freedom
## Multiple R-squared:  0.8264, Adjusted R-squared:  0.8144 
## F-statistic: 69.03 on 2 and 29 DF,  p-value: 9.395e-12