solutions - STA 6866 Fall 2011 Solutions Andrew Womack...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: STA 6866 Fall 2011 Solutions Andrew Womack & Luis Leon-Novello Assignment 1: Assigned Aug 22, Due Sep 7 Exercise 1.4: We can best describe the differences between order() and rank() through their use. Both take in a numeric vector. order() returns a vector where order(x)[i] is the index of the i- th smallest component of x while rank(x)[i] returns a vector where rank(x)[i] is the rank of the i- th component of x . > x<-c(4,1,3,6,2,5) > order(x) [1] 2 5 3 1 6 4 > rank(x) [1] 4 1 3 6 2 5 When using the function rep() the input times=k will repeat the input vector k times, the input each=k will repeat each element of the input vector k times with the first element first and so on, and the input length.out=k will cycle through the elements of the input vector in order until a vector or length k is created. > rep(c(1,2,3),times=3) [1] 1 2 3 1 2 3 1 2 3 > rep(c(1,2,3),each=3) [1] 1 1 1 2 2 2 3 3 3 > rep(c(1,2,3),length.out=7) [1] 1 2 3 1 2 3 1 Exercise 1.10: The logical opertors & and | perform element-wise comparison of two logical vectors. The logical opertors && and || perform comparison of only first element of two logical vectors. xor(,) performs the XOR logical operation element-wise on two logical vectors. XOR(A,B) is 1 if A or B is 1 but not both A and B are 1. > x<-c(1,0,1,0) > y<-c(1,1,0,0) > w<-c(0,1,0,1) 1 > x&y [1] TRUE FALSE FALSE FALSE > x&&y [1] TRUE > x|y [1] TRUE TRUE TRUE FALSE > x||y [1] TRUE > x&&w [1] FALSE > x||w [1] TRUE > xor(x,y) [1] FALSE TRUE TRUE FALSE > xor(x,w) [1] TRUE TRUE TRUE TRUE > xor(y,w) [1] TRUE FALSE FALSE TRUE Exercise 1.12: > x<-c(NA,1:5,NA,"a","n","d",NA,seq(pi,2*pi,le=4),NA) > (y<-is.na(x)) [1] TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE [12] FALSE FALSE FALSE FALSE TRUE > (w<-x[!is.na(x)]) [1] "1" "2" "3" [4] "4" "5" "a" [7] "n" "d" "3.14159265358979" [10] "4.18879020478639" "5.23598775598299" "6.28318530717959" Exercise 1.14: If we have a vector of data x of size 1 then var(x) returns NA because it computes the sample variance using length(x)-1 which would be 0. The option na.rm=T removes NA elements from x which does not effect the output if x has no missing data. > x<-1 > var(x) [1] NA > y<-c(rnorm(15),rep(NA,4)) > var(y) [1] NA > var(y,na.rm=T) [1] 1.238668 2 > var(y[!is.na(y)]) [1] 1.238668 > w<-c(x,NA) > var(w,na.rm=T) [1] NA The option na.action in the function lm and glm provides various controls for dealing with rows of a data frame that have missing data when fitting a linear model. na.omit Removes rows that contain missing values and does not pad residual and predicted output with NA s. na.exclude Removes rows that contain missing values and pads residual and predicted output with NA s....
View Full Document

This note was uploaded on 01/29/2012 for the course STAT 6866 taught by Professor Womack during the Fall '11 term at University of Florida.

Page1 / 17

solutions - STA 6866 Fall 2011 Solutions Andrew Womack...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online