R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # slr2.r > # first probe on SimStudy problem 2 > # normality of L1 regression > # > y <- c(4.5,5.5,6.5,8,10,12) > x <- (1:6) > # > g <- function(b) { + res <- y - b*x + med <- median(res) + g <- sum(abs(res-med)) } > # > # simple linear regression > slr <- function(y,x) { + xb <- mean(x) + yb <- mean(y) + sxx <- sum((x-xb)*(x-xb)) + b1 <- sum((x-xb)*(y-yb))/sxx + b0 <- yb - b1*xb + sse <- sum((y-b0-b1*x)*(y-b0-b1*x)) + slr <- c(b0,b1,sqrt(sse/((length(x)-2)*sxx))) } > # > rslt <- slr(y,x) > rslt [1] 2.5000000 1.5000000 0.1195229 > int <- c(rslt[2]-4*rslt[3],rslt[2]+4*rslt[3]) # +/- 4 se's > int [1] 1.021909 1.978091 > # use optimize on smoother function > this <- optimize(g,int) > this $minimum [1] 1.5 $objective [1] 2 > # different approach > c2 <- .1 > h <- function(u){sqrt(c2+u*u)} > hp <- function(u){u/sqrt(c2+u*u)} > hpp <- function(u){c2/((c2+u*u)*sqrt(c2+u*u))} > b <- c(1,1) > for ( i in(1:5) ) { # iterate on i + e <- y - b[1] - x*b[2] + print(e) + print(sapply(e,h)) + print(sum(sapply(e,h))) + hpatb <- sapply(e,hp) + hppatb <- sapply(e,hpp) + print(hpatb) + print(hppatb) + grad <- -c(sum(hpatb),sum(x*hpatb)) + print(grad) + hess <- matrix(c(sum(hppatb),sum(x*hppatb),sum(x*hppatb), + sum(x*x*hppatb)),2,2) + print(hess) + b <- b - solve(hess,grad) + print(c(i,b)) } [1] 2.5 2.5 2.5 3.0 4.0 5.0 [1] 2.519921 2.519921 2.519921 3.016621 4.012481 5.009990 [1] 19.59885 [1] 0.9920947 0.9920947 0.9920947 0.9944903 0.9968896 0.9980060 [1] 0.0062494157 0.0062494157 0.0062494157 0.0036428217 0.0015479652 [6] 0.0007952239 [1] -5.96567 -20.90301 [,1] [,2] [1,] 0.02473426 0.06457895 [2,] 0.06457895 0.21310416 [1] 1.00000 -70.40687 120.72734 [1] -45.82047 -165.54781 -285.27516 -404.50250 -523.22984 -641.95718 [1] 45.82156 165.54811 285.27533 404.50262 523.22994 641.95726 [1] 2066.335 [1] -0.9999762 -0.9999982 -0.9999994 -0.9999997 -0.9999998 -0.9999999 [1] 1.039418e-06 2.204080e-08 4.307322e-09 1.510901e-09 6.981074e-10 [6] 3.779912e-10 [1] 5.999973 20.999968 [,1] [,2] [1,] 1.068353e-06 1.108224e-06 [2,] 1.108224e-06 1.221582e-06 [1] 2 207266770 -205223933 [1] -2042833 203181101 408405036 613628970 818852906 1024076841 [1] 2042833 203181101 408405036 613628970 818852906 1024076841 [1] 3070187687 [1] -1 1 1 1 1 1 [1] 1.173010e-20 1.192203e-26 1.468002e-27 4.327952e-28 1.821303e-28 [6] 9.311129e-29 [1] -4 -19 [,1] [,2] [1,] 1.173011e-20 1.173013e-20 [2,] 1.173013e-20 1.173017e-20 [1] 3.000000e+00 -5.569785e+26 5.569780e+26 [1] 4.828466e+20 -5.569775e+26 -1.113956e+27 -1.670934e+27 -2.227912e+27 [6] -2.784890e+27 [1] 4.828466e+20 5.569775e+26 1.113956e+27 1.670934e+27 2.227912e+27 [6] 2.784890e+27 [1] 8.354668e+27 [1] 1 -1 -1 -1 -1 -1 [1] 8.883263e-64 5.787446e-82 7.234298e-83 2.143495e-83 9.042867e-84 [6] 4.629947e-84 [1] 4 19 [,1] [,2] [1,] 8.883263e-64 8.883263e-64 [2,] 8.883263e-64 8.883263e-64