New version page

HARVARD STAT 335 - S-Plus

Documents in this Course
Load more

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

View Full Document
View Full Document

End of preview. Want to read all 34 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

12 S CHAPTER return.DataS CHAPTER.DataS CHAPTERMySwork .Data3 S –e >return;return+#4 setenv S_CLEDITOR emacs5 > 4 + 5 / 3 ## addition & divison [1] 5.666667 > (4 + 5) / 3 ## using parentheses [1] 3 > log(10) ## natural logarithm [1] 2.302585 <- _ <- > xbar <- (4 + 6 + 8 + 10)/4 ## assigns avg to xbar > xbar ## the object xbar [1] 7 > s <- sqrt(((-3)^2 + (-1)^2 + 1^2 + 3^2)/3) > s [1] 2.581989 > z <- (xbar - 10)/s > z [1] -1.1618956 sqrtls()> ls() [1] ".Last.value" "cars" "cityfuel" [4] "fuel.lm" "highfuel" "mpg.lm" [7] "s" "xbar" "z" rm()> rm(s,z) > ls() [1] ".Last.value" "cars" "cityfuel" [4] "fuel.lm" "highfuel" "mpg.lm" [7] "xbar" q()s z7 > data <- c(4,6,8,10) ## concatenating > data ## print data [1] 4 6 8 10 > length(data) ## length of data [1] 4 > data[3] ## 3rd element [1] 8 > ind <- 2:3 ## sequence > ind [1] 2 3 > data[ind] ## subset [1] 6 8 > data + 100 [1] 104 106 108 110 > sqrt(data) ## square root [1] 2.000000 2.449490 2.828427 3.162278 > sum(data) ## sum of elements [1] 288 > xbar <- mean(data) ## mean of data > s <- sqrt( sum((data-xbar)^2) / (length(data)-1) ) > s.easy<-stdev(data) > s [1] 2.581989 > s.easy [1] 2.581989 > z<-(xbar-10)/s > z [1] -1.1618959 > example.l $id: [1] 1 2 3 4 $data: [1] 4 6 8 10 $xbar: [1] 7 $sdev: [1] 2.581989 > names(example.l) ## the names [1] "id" "data" "xbar" "sdev" > example.l$data ## item data of list [1] 4 6 8 10 > example.l$data[2:3] ## elements 2-3 of data [1] 6 810 > mat<-cbind(id=1:5,iq=rnorm(5,mean=100,sd=15)) > mat id iq [1,] 1 66.57908 [2,] 2 118.39228 [3,] 3 123.40596 [4,] 4 92.13515 [5,] 5 106.26696 > dim(mat) ## dimensions of mat [1] 5 2 > dimnames(mat) ## row and column names [[1]]: character(0) [[2]]: [1] "id" "iq"11 > mat[1:2,] ## first 2 rows of mat id iq 1 66.57908 2 118.39228 > mat[,2] ## 2nd column, by number 66.57908 118.3923 123.406 92.13515 106.267 > mat[,'iq'] ## 2nd column, by name 66.57908 118.3923 123.406 92.13515 106.267 > mat.2 <- mat[-5,] ## remove last line > mat.2 id iq 1 66.57908 2 118.39228 3 123.40596 4 92.1351512 > ind <- mat[,'iq'] > 115 ## iq’s > 115 > ind ## vector of logicals (True/False) F T T F F > mat.3 <- mat[ind,] > mat.3 id iq 2 118.3923 3 123.4060 <== <= >=!= &| !> mat[!( (mat[,2]>85) & (mat[,2]<115) ),] id iq 1 66.57908 2 118.39228 3 123.4059613 abs, log, sqrt, sin, asin. t (transpose), %*% (matrix mult), solve, qr (qr decomposition). mean, median, var, quantile, summary, lm (linear model). dnorm pnorm qnorm rnormdt dfdchisq χ2dgamma dexpdbinom14 > my.ttest function(x, null = 0) { n <- length(x) xbar <- mean(x) sd <- stdev(x) se <- sd/sqrt(n) tstat <- (xbar - null)/se tstat } > my.ttest(data) [1] 5.42217715 > t.test(data) One-sample t-Test data: data t = 5.4222, df = 3, p-value = 0.0123 alternative hypothesis: true mean is not equal to 0 95 percent confidence interval: 2.891479 11.108521 sample estimates: mean of x 716 /home/irwin/Scourse/93cars.datdata.frame read.tabledata.frame> cars<-read.table(“/home/irwin/Scourse/93cars.dat” + , header=T,row.names=NULL) cars.df> attach(“/home/irwin/Scourse/.Data”)17 > dim(cars.df) [1] 93 10 > class(cars.df) [1] "data.frame" > names(cars.df) [1] "manu" "model" "cylinders" "type" "engsize" "weight" [7] "citympg" "cityfuel" "highmpg" "highfuel" > cars.df[1:5,] manu model cylinders type engsize weight citympg cityfuel 1 Acura Integra 4 Small 1.8 2705 25 4.000000 2 Acura Legend 6 Midsize 3.2 3560 18 5.555556 3 Audi 90 6 Compact 2.8 3375 20 5.000000 4 Audi 100 6 Midsize 2.8 3405 19 5.263158 5 BMW 535i 4 Midsize 3.5 3640 22 4.545455 highmpg highfuel 1 31 3.225806 2 25 4.000000 3 26 3.846154 4 26 3.846154 5 30 3.33333318 > summary(cars.df) manu model cylinders type engsize Ford: 8 Vision: 1 *: 1 Compact:16 Min.:1.000 Chevrolet: 8 Town_Car: 1 3: 3 Large:11 1st Qu.:1.800 Dodge: 6 Tercel: 1 4:49 Midsize:22 Median:2.400 Pontiac: 5 Tempo: 1 5: 2 Small:21 Mean:2.668 Mazda: 5 Taurus: 1 6:31 Sporty:14 3rd Qu.:3.300 Volkswagen: 4 Swift: 1 8: 7 Van: 9 Max.:5.700 (Other):57 (Other):87 weight citympg cityfuel highmpg Min.:1695 Min.:15.00 Min.:2.174 Min.:20.00 1st Qu.:2620 1st Qu.:18.00 1st Qu.:4.000 1st Qu.:26.00 Median:3040 Median:21.00 Median:4.762 Median:28.00 Mean:3073 Mean:22.37 Mean:4.699 Mean:29.09 3rd Qu.:3525 3rd Qu.:25.00 3rd Qu.:5.556 3rd Qu.:31.00 Max.:4105 Max.:46.00 Max.:6.667 Max.:50.00 highfuel Min.:2.000 1st Qu.:3.226 Median:3.571 Mean:3.541 3rd Qu.:3.846 Max.:5.000 > postscript(“city.plot.ps”,horiz=F) ## put plot in file > par(mfrow=c(5,2)) ## 5 rows, 2 columns > plot(cars.df,ask=F) ## generate summary plot for each variable > dev.off() ## close graphics device1920 Citympg Weight> postscript("citympg.ps",horiz=T) > plot(cars.df$weight,cars.df$citympg, + xlab="Weight",ylab="CityMPG", + main="City MPG versus Weight") > abline(lsfit(cars.df$weight,cars.df$citympg)) > dev.off() postscript() motif()2122 > citympg.lm <- lm(citympg ~ weight, data = cars.df) > citympg.lm Call: lm(formula = citympg ~ weight, data = cars.df) Coefficients: (Intercept) weight 47.04835 -0.008032392 Degrees of freedom: 93 total; 91 residual Residual standard error: 3.03831 > summary(citympg.lm) Call: lm(formula = citympg ~ weight, data = cars.df) Residuals: Min 1Q Median 3Q Max -6.795 -1.971 0.02486 1.186 13.83 Coefficients: Value Std. Error t value Pr(>|t|) (Intercept) 47.0484 1.6799 28.0064 0.0000 weight -0.0080 0.0005 -14.9583 0.0000 Residual standard error: 3.038 on 91 degrees of freedom Multiple R-Squared: 0.7109 F-statistic: 223.8 on 1 and 91 degrees


View Full Document
Loading Unlocking...
Login

Join to view S-Plus and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view S-Plus and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?