#Načítanie dát Yt <- c(6.1, 7.3, 9.6, 10.2, 10.1, 11.3, 12.2, 12.5, 13.2) Pt <- c(103, 102, 100, 94, 98, 97, 98, 97, 96) It <- c(110, 114, 130, 135, 141, 152, 160, 165, 170) #Výpočet vektora parametrov b cez funkciu lm() regresia = lm(Yt ~ cbind(Pt,It)) #VYPIS regresia #PODROBNEJSI VYPIS summary(regresia) #Výpočet vektora parametrov b cez vlastnú funkciu #vlastná funkcia prináša aj grafické zobrazenie reg = function (Yt, m) { regr = lm (Yt~m) nr = nrow(m) nc = ncol(m) y = c() b = c() for (i in 1:(nc+1)) { b[i]=regr$coef[i] } for (i in 1:nr) { ypom = 0; for (j in 2:(nc+1)) { ypom = ypom + b[j]*m[i,(j-1)] } y[i]=b[1]+ypom } plot (Yt, type = "o", col="green",xlab="cas t",ylab="hodnoty y", main = "Linearna regresia") lines (y, type = "o", col="red") cat("Linear regresion","\n", "--------------------------------------","\n", "Estimated values of Y:","\n","\n", " ",y,"\n", "--------------------------------------","\n", "Summary from lm function:","\n" ) summary (regr) } #volanie funkcie reg(Yt,cbind (Pt,It)) #Výpočet vektora parametrov b cez matice pom = rep(1,9) X<-matrix(c(pom,Pt,It),9,3) X = array( c(pom,Pt,It) , c(9,3) ) X Xtran = t(X) XXinv = solve(Xtran %*% X) b = XXinv %*% Xtran %*% Yt #výpis výsledkov b