Kapitel 53 Einfachste Graphiken erstellen (ohne ggplot2)

53.1 Wir können auch einen Vektor in ein Objekt speichern

R bietet unterschiedliche Formen an http://www.endmemo.com/program/R/pchsymbols.php

x <- c(3,4,2,5,4,3,2,6,5,3,1,2,3,4,5)
hist(x, col="PINK")

y<-c(2,3,4,2,6,5,3,2,4,5,7,8,6,3,1)
plot(x,y, col="red")

## oder etwas schöner...
plot(x,y,pch=1, cex=4)

## ah, das ist zu gross...

plot(x,y,pch=1, cex=0.5)

## ah, das ist jetzt zu klein

plot(x,y,pch=1, cex=2)

## ah, das ist zu hässlich

plot(x,y,pch=1, cex=4, col="PINK")

## ah, da sieht man ja nichts

plot(x,y,pch=20, cex=3, col="PINK")

53.1.1 Wir können Grafiken kombinieren, z.B. um eine Regressionslinie hinzuzufügen, man kann sogar den Determinationskoeffizienten (r2) hinzufügen.

http://www.ats.ucla.edu/stat/r/faq/scatter.htm

plot.new()
plot(x,y)
reg1 <- lm(y~x) ## si le plot est (a,b), il faut mettre faire la regression b~a; parce que a = axe horizontale, b = axe verticale; et par definition on dit que a explique b
abline(reg1)
summary(reg1)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4952 -1.0361  0.2596  1.0048  2.5865 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   7.2500     1.1693   6.200 3.22e-05 ***
## x            -0.9183     0.3140  -2.924   0.0118 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.654 on 13 degrees of freedom
## Multiple R-squared:  0.3968, Adjusted R-squared:  0.3504 
## F-statistic: 8.552 on 1 and 13 DF,  p-value: 0.01184
cor(y,x)^2
## [1] 0.3968086
legend("topright", bty="n", legend=paste("r-squared: ",  format(summary(reg1)$adj.r.squared, digits=3)))

53.1.2 Wir können auch die Regressionsformel anfügen:

fit <- lm(y ~ x)
summary(fit)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4952 -1.0361  0.2596  1.0048  2.5865 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   7.2500     1.1693   6.200 3.22e-05 ***
## x            -0.9183     0.3140  -2.924   0.0118 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.654 on 13 degrees of freedom
## Multiple R-squared:  0.3968, Adjusted R-squared:  0.3504 
## F-statistic: 8.552 on 1 and 13 DF,  p-value: 0.01184
plot.new()
plot(y ~ x, xlab = "X", ylab = "Y")
abline(coef(fit)[1:2])
cf <- round(coef(fit), 2) # round = runden, hier auf zwei Nachkommastellen.

## sign check to avoid having plus followed by minus for negative coefficients
eq <- paste0("Y = ", cf[1],
             ifelse(sign(cf[2])==1, " + ", " - "), abs(cf[2]), "\u00D7x ")
## printing of the equation
mtext(eq, 3, line=-2)

53.1.3 Im nächsten Beispiel eine Regression mit mehreren unabhängigen Variablen.

plot.new()
covariable<-c(5,2,1,4,7,3,2,7,2,5,7,3,9,3,1)
fit <- lm(y ~ x+covariable)
summary(fit)
## 
## Call:
## lm(formula = y ~ x + covariable)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.7629 -0.6534 -0.1574  0.5317  2.9236 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   5.9980     1.2855   4.666 0.000545 ***
## x            -0.9027     0.2902  -3.110 0.009019 ** 
## covariable    0.2946     0.1639   1.797 0.097534 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.528 on 12 degrees of freedom
## Multiple R-squared:  0.5247, Adjusted R-squared:  0.4455 
## F-statistic: 6.624 on 2 and 12 DF,  p-value: 0.01153
plot.new()
plot(y ~ x, xlab = "X", ylab = "Y")
abline(coef(fit)[1:2])

## rounded coefficients for better output
cf <- round(coef(fit), 2) 

## sign check to avoid having plus followed by minus for negative coefficients
eq <- paste0("Y = ", cf[1],
             ifelse(sign(cf[2])==1, " + ", " - "), abs(cf[2]), " \u00D7x ",
            ifelse(sign(cf[3])==1, " + ", " - "), abs(cf[3]), " \u00D7Covariable")
## printing of the equation
mtext(eq, 3, line=-2)