#zuweisen, abfragen, rechnen 3+4 2*pi x<-4 x x<-c(1,3,5,2) x x<-3:7 x<-seq(1,3,0.2) x x<-seq(1,7,length = 21) x #einfache Funktionen mean(x) sum(x) var(x) sd(x) #Kombinatorik factorial(5) factorial(100) choose(4,2) choose(12,3) #Verteilungen diskret dhyper(4,10,12,6) #vier Erfolge, bei 10 Treffern, 12 Nieten und 6 Versuchen dbinom(3,13,0.17) pbinom(3,13,0.17) sum(dbinom(0:3,13,0.17)) pbinom(7,13, 0.17) - pbinom(2,13,0.17) sum(dbinom(3:7,13,0.17)) dpois(14,20) ppois(14,20) ppois(30,20) #Approximation grafisch plot(pbinom(0:20,20,0.33), type = "l", ylab ="Wahrscheinlichkeit") lines(phyper(0:20,7,14,20), col = "blue") lines(phyper(0:20,10,20,20), col = "green") lines(phyper(0:20,20,40,20), col = "yellow") lines(phyper(0:20,50,100,20), col = "red") #plot plot(ppois(0:24,4), type = "l", ylab ="Wahrscheinlichkeit") lines(pbinom(0:24,24,1/6), col = "blue") #Verteilungen stetig pnorm(0) dnorm(0) pnorm(0,1,4) pnorm(2) pnorm(2) - pnorm(-2) punif(4,2,12) dunif(seq(0,1,0.1)) plot(dnorm, xlim = c(-4,4)) plot(pnorm, xlim = c(-4,4)) plot(dexp, xlim = c(0,5)) lines(seq(0,5,0.01),dexp(seq(0,5,0.01), rate = 0.5), type = "l", col = "green") lines(seq(0,5,0.01),dexp(seq(0,5,0.01), rate = 5), type = "l", col = "red") plot(pexp, xlim = c(0,5)) lines(seq(0,5,0.01),pexp(seq(0,5,0.01), rate = 0.5), type = "l", col = "green") lines(seq(0,5,0.01),pexp(seq(0,5,0.01), rate = 5), type = "l", col = "red") # Konzepte x<-runif(100,0,5) mean(x) var(x) ((5-0)^2)/12 sd(x) y<-rnorm(100,2,3) plot(x,y, pch =19, xlab = "Gleichverteilung", ylab ="Normalverteilung") cov(x,y) cor(x,y) z<-x+rnorm(100,0,1) plot(x,z, pch =19, xlab = "Gleichverteilung", ylab ="Gleichverteilung mit Rauschen") cov(x,z) cor(x,z) #Grafiken library(reshape) data(tips) attach(tips) head(tips) #Barchart barplot(table(size), col = "grey") #Streudiagram plot(total_bill, tip, pch = 19, xlab = "Gesamtrechnung", ylab = "Trinkgeld") points(total_bill[total_bill<10], tip[total_bill < 10], col = "magenta", pch =19) points(total_bill[total_bill<10 & tip > 5], tip[total_bill < 10 & tip > 5], col = "green", pch =19) #Histogram hist(tip, col = "grey") hist(tip, col = "grey", breaks = 15) #Ordnungsaufgabe x<-vector(length = 10000) y<-vector(length = 10000) for(i in 1:10000){ v<-runif(5) v<-sort(v) x[i]<-v[5] y[i]<-v[4] } mean(x) mean(y) for(i in 1:10000){ v<-runif(20) v<-sort(v) x[i]<-v[20] y[i]<-v[19] } mean(x) mean(y) for(i in 1:10000){ v<-runif(100) v<-sort(v) x[i]<-v[100] y[i]<-v[99] } mean(x) mean(y)