remove(list=ls()) set.seed(13) ek<-function(k,x,len){ if(k==0){sqrt(1/len)} else{ if(k%%2==1){ sqrt(2/len)*sin(pi*(k+1)*x/len) } else{ sqrt(2/len)*cos(pi*k*x/len) } } } vek<-Vectorize(ek,c("k","x")) fNhat<-function(N,dat,x,l=(max(dat)-min(dat))){ n<-length(dat) sum_p1<-outer(0:N,dat,vek,len=l) sum_p2<-sapply(0:N,ek,x=x,len=l) sum_p2<-outer(sum_p2,rep(1,n)) sum_prod<-sum_p1*sum_p2 sum_ges<-sum(sum_prod) sum_ges/n } dat<-runif(500) N<-20 x<-seq(from=min(dat),to=max(dat),len=100) res<-sapply(x,fNhat,N=N,dat=dat) hist(dat,freq=FALSE) lines(x,res) ####Aufgabe 2#### student<-read.table("C:/Users/Chris/Documents/Stochastik III/R/student.csv",header=T,sep=";") n<-1:length(student[,1]) M<-5000 alpha<-0.05 corstar<-1:M for(m in 1:M){ index<-sample(n,30,replace=TRUE) studentstar<-student[index,] corstar[m]<-cor(studentstar$Uebung,studentstar$Klausur) } var(corstar) hist(corstar,freq=FALSE) quantile(corstar,probs=c(alpha/2,1-alpha/2))