Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##############
- ### 2a. model kvadraticky - parabola
- x = c(1, 1.5, 2, 2, 2.5)
- y = c(3, 5, 6, 6, 7)
- vys2 <- lm(y ~ 1 + x + I(x^2))
- ## <> lm( y ~ poly(x,2) ) # - orthogonal
- plot(x,y)
- lines(x, vys2$fit)
- # <=>:
- par = function(x, c) {
- c[1] + c[2]*x + c[3]*x^2
- }
- xx = seq(min(x), max(x), .5); ## FromToBy
- lines(xx, par(xx, vys2$coef), col = "blue" )
- xx = seq(min(x), max(x), .2); ## FromToBy
- lines(xx, par(xx, vys2$coef), col = "blue" )
- xx = seq(min(x), max(x), .1); ## FromToBy
- lines(xx, par(xx, vys2$coef), col = "magenta" )
- ##############
- ### 2b. model maticovo - Imre
- x = c(1, 1.5, 2, 2, 2.5)
- y = c(3, 5, 6, 6, 7)
- #X = cbind( rep(1,length(x)), x, x^2)
- X = cbind( rep(1,length(x)), x)
- ### a) pomocou inverznej matice - pre teoriu
- solve( t(X) %*% X) %*% t(X) %*% y # - coeff
- ### b) ako riesenie n.sustavy - pre aplikacie
- solve( t(X) %*% X, t(X) %*% y ) # - coeff
- # <=> :
- r2 = lm(y ~ 1 + x + I(x^2));
- r2$coeff
- r2$coeff[3]
- summary(r2)
- #=============
- # Linearizovatelne nelinearne modely
- x = c( 45.3, 96.5, 164.8, 260.6, 429.1, 596.6, 775.1)
- y = c(8886.09, 8643.75, 8340.46, 8126.6, 7486.71, 7370.64, 7258.11)
- plot(y~x)
- z1=exp(-x);v1 = lm(y~z1);v1
- z2=1/x; v2 = lm(y~z2)
- z3=log(x); v3 = lm(y~z3);
- summary(v1)
- summary(v2)
- summary(v3)
- anova(v1)$'Pr(>F)'[1]
- anova(v2)$'Pr(>F)'[1]
- anova(v3)$'Pr(>F)'[1]
- lines(x, v1$coef[1]+v1$coef[2]*exp(-x),col="green")
- lines(x, v2$coef[1]+v2$coef[2]/x,col="yellow")
- lines(x, v3$coef[1]+v3$coef[2]*log(x),col="green")# <=>
- lines(x, v3$fit, col="magenta")
- ### Kvadraticky model:
- v4 = lm(y~x+I(x^2))
- lines(x, v4$fit, col="blue")
- anova(v3)$'Pr(>F)'[1]
- anova(v4)$'Pr(>F)'[1]
- ### Rezudualny sucet stvorcov
- ### Pouzivaju to aj numerici:
- sum((v1$resid)^2)
- sum((v2$resid)^2)
- sum((v3$resid)^2)
- sum((v4$resid)^2)
- ####
- # vys_ = lm(y ~ 1 + x + I(x^2) + I(x^3)) # NONO
- # summary(vys_)
- # !!!!!!!!!!!!!!!!!! MEGOLDAS
- vys3_1 = lm(y ~ 1 + I(x^2) + I(x^3))
- summary(vys3_1)
- # alebo este lepsie:
- vys3_2 = lm(y ~ 1 + x + I(x^3))
- summary(vys3_2)
- lines(x, vys3_2$fit, col='gold')
- vys3_2$fit
- ### =============
- ### Teploty v case t
- ### !!! http://pastebin.com
- t = c( 7, 9, 11, 13, 15, 17, 19, 21, 23)
- o = c( 8.2, 10.7, 18.9, 26.7, 24.6, 13, 11.4, 10.6, 10.2)
- r = c( 38.4, 38.5, 35.8, 35.8, 37, 38.4, 38.2, 37.6, 38.1)
- s = c( 13.5, 13.6, 15.3, 17.2, 18.1, 17.9, 16.7, 15.8, 15.4)
- i = c( 15.6, 16, 17.7, 19.7, 20.4, 19.7, 18.6, 17.9, 17.4)
- reg = lm(i ~ o + r + s)
- summary(reg)
- df = data.frame(t=t,o=o,r=r,s=s,i=i);
- options(digits=4)
- cor(df)
- options(digits=7)
- ########
- ### MS
- x1 = c(2310,2333,2356,2379,2402,2425,2448,2471,2494,2517,2540)
- x2 = c(2,2,3,3,2,4,2,2,3,4,2)
- x3 = c(2,2,1.5,2,3,2,1.5,2,3,4,3)
- x4 = c(20,12,33,43,53,23,99,34,23,55,22)
- y = c(142000,144000,151000,150000,139000,169000,126000,142900,163000,169000,149000)
- r2 = lm(y ~ 1 + x1 + x2 + x3 + x4); r2$coeff
- summary(r2)
- ##############
- ### 3. model sinus:
- ### 3. model sinus:
- x = c(1, 1.5, 2, 2, 2.5)
- y = c(3, 5, 6, 6, 5)
- x1=pi/2*(x-1)
- y3=y-3
- plot(y3~x1)
- r = lm(y3 ~ sin(x1))
- summary(r)
- lines(x1, r$fit)
- plot(y~x)
- lines((2*x1)/pi + 1, r$fit + 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement