wohnflaeche <- c (80 , 120 , 100 , 150 , 200 , 90 , 110 , 140 , 180 , 160 )
verkaufspreis <- c (300 , 480 , 400 , 600 , 800 , 350 , 440 , 560 , 720 , 640 )
df_haeuser <- data.frame (wohnflaeche,
verkaufspreis)
modell <- lm (verkaufspreis ~ wohnflaeche, data = df_haeuser)
summary (modell)
Call:
lm(formula = verkaufspreis ~ wohnflaeche, data = df_haeuser)
Residuals:
Min 1Q Median 3Q Max
-11.4426 -2.3505 0.6932 3.8388 6.4602
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -16.94581 6.53137 -2.595 0.0319 *
wohnflaeche 4.10486 0.04725 86.881 3.44e-13 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.632 on 8 degrees of freedom
Multiple R-squared: 0.9989, Adjusted R-squared: 0.9988
F-statistic: 7548 on 1 and 8 DF, p-value: 3.437e-13
predict (modell, newdata = data.frame (wohnflaeche = c (130 , 150 , 170 )))
1 2 3
516.6854 598.7825 680.8797
df_icecream <- data.frame (
Eismenge = c (2000 ,2000 ,6000 ,4000 ,6000 ,
4000 ,4000 ,7000 ,7000 ,8000 ),
Temperatur = c (10 , 15 , 20 , 15 , 25 ,
25 , 30 , 30 , 40 , 40 ),
Niederschlag = c (25 ,20 ,15 ,20 ,10 ,8 ,1 ,1 ,0 ,0 ),
Wochentag = factor (c ("Mittwoch" , "Freitag" , "Sonntag" ,
"Freitag" ,"Sonntag" , "Mittwoch" ,
"Freitag" , "Sonntag" , "Freitag" ,
"Sonntag" ),
levels = c ("Mittwoch" ,
"Freitag" ,
"Sonntag" ))
)
# Regressionsmodell mit kategoriellem Merkmal
lm_model <- lm (Eismenge ~ ., data = df_icecream)
summary (lm_model)
Call:
lm(formula = Eismenge ~ ., data = df_icecream)
Residuals:
1 2 3 4 5 6 7 8 9 10
-155.5 -996.1 349.7 1003.9 -267.2 155.5 -523.7 439.0 515.9 -521.5
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1904.45 3111.82 -0.612 0.5673
Temperatur 204.12 88.24 2.313 0.0686 .
Niederschlag 80.75 94.42 0.855 0.4315
WochentagFreitag 223.80 731.48 0.306 0.7720
WochentagSonntag 2261.17 769.61 2.938 0.0323 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 805.9 on 5 degrees of freedom
Multiple R-squared: 0.9188, Adjusted R-squared: 0.8539
F-statistic: 14.15 on 4 and 5 DF, p-value: 0.00619
newdata <- data.frame (Temperatur = c (20 , 25 ),
Niederschlag = c (10 , 15 ),
Wochentag = c ("Mittwoch" , "Freitag" ))
predict (lm_model, newdata = newdata)
automarke_chr <- c ("Opel" , "BMW" , "Mercedes" , "Mercedes" , "Opel" , "BMW" )
automarke_factor <- factor (automarke_chr,
levels = c ("Opel" , "VW" , "Mercedes" , "BMW" ),
ordered = TRUE )
automarke_chr
[1] "Opel" "BMW" "Mercedes" "Mercedes" "Opel" "BMW"
tafel <- table (automatik = mtcars$ am, gaenge = mtcars$ gear)
chisq.test (tafel)
Warning in chisq.test(tafel): Chi-squared approximation may be incorrect
Pearson's Chi-squared test
data: tafel
X-squared = 20.945, df = 2, p-value = 2.831e-05
kt_zoo <- matrix (c (90 ,10 ,60 ,40 ,50 ,150 ),
ncol = 2 , byrow= TRUE )
chisq.test (kt_zoo)
Pearson's Chi-squared test
data: kt_zoo
X-squared = 118, df = 2, p-value < 2.2e-16
df_zoo <- data.frame (
Besuche = factor (c (rep ("1-2" , 90 ),
rep ("3-4" , 60 ),
rep (">4" , 50 ),
rep ("1-2" , 10 ),
rep ("3-4" , 40 ),
rep (">4" , 150 )),
levels = c ("1-2" , "3-4" , ">4" )),
Jahreskarte = factor (rep (c ("nein" ,"ja" ),
each = 200 ),
levels = c ("nein" ,"ja" ))
)