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. > # slr1.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(b1,sqrt(sse/((length(x)-2)*sxx))) } > # > rslt <- slr(y,x) > rslt [1] 1.5000000 0.1195229 > int <- c(rslt[1]-4*rslt[2],rslt[1]+4*rslt[2]) # +/- 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 > # done > rm(list=ls()) > q()