R version 2.10.1 (2009-12-14) 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. [R.app GUI 1.31 (5537) x86_64-apple-darwin9.8.0] > install.packages('UsingR') --- Please select a CRAN mirror for use in this session --- trying URL 'http://www.revolution-computing.com/cran/bin/macosx/leopard/contrib/2.10/UsingR_0.1-12.tgz' Content type 'application/x-gzip' length 1143093 bytes (1.1 Mb) opened URL ================================================== downloaded 1.1 Mb The downloaded packages are in /var/folders/vX/vXSkZhgYGjmQee019Jt4OU+++TY/-Tmp-//RtmpE0JDzT/downloaded_packages > library(UsingR) > x=homedata$y1970 > y=homedata$y2000 > plot(x,y) > cor(x,y) [1] 0.8962155 > help(cor) starting httpd help server ... done > cor(x,y,method='pearson') [1] 0.8962155 > cor(x,y,method='spearman') [1] 0.8878185 > sum( ((x-mean(x))/sd(x)) * ((y-mean(y))/sd(y)) ) / (length(x)-1) [1] 0.8962155 > mean( ((x-mean(x))/sd(x)) * ((y-mean(y))/sd(y)) ) [1] 0.8960845 > mean( ((x-mean(x))/sd(x)) * ((y-mean(y))/sd(y)) ) [1] 0.8960845 > cor(x,y,method='kendall') [1] 0.7240265 > x=rnorm(1000) > y=rnorm(1000) > plot(x,y) > par(mfrow=c(2,1)) > plot(x,y) > qqplot(x,y) > y=cfb$VEHIC > hist(x) > hist(y) > par(mfrow=c(1,1)) > qqplot(x,y) > x=jitter(1:100,50) > y=jitter(1:100,50) > plot(x,y) > model=lm(y~x) > abline(model) > coef(model) (Intercept) x 1.9966944 0.9653584 > params=coef(model) > names(params)=c('b','m') > params b m 1.9966944 0.9653584 > x=homedata$y1970 > y=homedata$y2000 > plot(x,y) > model=lm(y~x) > abline(model) > z=identify(x,y,n=1) > c(x[6655],y[6655]) [1] 149700 360000 > c(x[z],y[z]) [1] 149700 360000 > params=coef(model) > names(params)=c('b','m') > predicted=params['m']*x[z] + params['b'] > predicted m 683113.7 > params b m -1.040047e+05 5.257972e+00 > predict(model,data.frame(x=x[z])) 1 683113.7 > y[z]-predicted m -323113.7 > residuals(model)[x==x[z]] 4742 6655 -73913.73 -323113.73 > sum(residuals(model)^2) [1] 2.300683e+13 > sum(residuals(model)^2)/length(x) [1] 3363079303 > sum( (y-predict(model,data.frame(x)))^2 ) [1] 2.300683e+13 > x=jitter(1:100,50) > y=jitter((1:100)^2,50) > plot(x,y) > model=lm(y~x) > abline(model) > sum(residuals(model)^2) [1] 92459073 > sum(residuals(model)^2)/length(x) [1] 924590.7 > y2=y > y2[y2<0]=0 > plot(x,sqrt(y2)) > model = lm(sqrt(y2)~x) > abline(model) > sum(residuals(model)^2)/length(x) [1] 38.5025 > sum( (y - predict(model,data.frame(x))^2 )^2 )/length(x) [1] 426696.3 > x=emissions$perCapita > y=emissions$CO2 > plot(x,y) > model=lm(y~x) > abline(model) > model=lm(y~x,subset=-1) > abline(model,lty=2) > library(MASS) > abline(lqs(y~x,data=emissions),lty=3) > abline(rlm(y~x,data=emissions,method="MM"),lty=5) > x=five.yr.temperature$days > y=five.yr.temperature$temps > plot(x,y) > scatter.smooth(y~x,col='gray') > lines(smooth.spline(y~x),col='blue') > lines(supsmu(x,y),col='red') > legend(80,lty=rep(1,3),col=c('black','blue','red'),legend=c('scatter.smooth','smooth.spline','supsmu'))