############### HOMEWORK 1, STAT 541, DUE MON, SEPT 10, 2007 ############### # Student Name: # Rules of the game: # 1) You can discuss the homework with each other in general terms, # but you must write your own solutions and not copy from anyone. # 2) Your R code is not permitted to use loops or the 'apply' function. # 3) Work your way through the R intro up to the point before 'LISTS' # but including 'ARRAYS'. # 4) Edit your answer into this file following each "ANSWER:". # 5) Send questions and your final solutions to stat541.at.wharton[at-sign]gmail.com #PROBLEM 0: # Set yourself up with R on your computer. # Follow the instructions in Stat-541-R-intro.R from the class website. #PROBLEM 1: # What happens in this expression? 1:10 - 0:1 #ANSWER: #PROBLEM 2: # Create a vector consisting of the numbers 1 to N, where 1 appears # once, 2 appears twice, 2 appears 3 times,... # Show results for N=5 #ANSWER: #PROBLEM 3: # Evaluate the expressions: -3:5; (-3):5; -(3:5) # What does this tell you about operator precedence of '-' and ':'? # By comparison, evaluate this: 1-3:5; 1-(-3):5; 1-(3:5) # Comment? If you had been the creator of R, # would you have designed operator precedence this way? #ANSWER: #PROBLEM 4: # Create a vector of integers between 1 and N # that are neither divisible by 3 nor by 5. # Show results for N=100 #ANSWER: #PROBLEM 5: # Create a vector of length N that contains one plus # the cumulative sums of reciprocals of cumulative products # of the integers 1,2,...,N. Show results for N=100. # What does the vector converge to? # (Find functions for cumulative products and sums.) #ANSWER: #PROBLEM 6: # Create a vector of vowels and a vector of consonants # from the standard dataset 'letters' or 'LETTERS'. # Then create code that generates a random 3-letter word # built form a random vowel, a random consonant, and # another a random vowel. #ANSWER: #PROBLEM 7: # Create a vector containing all possible pairs # of consonants and vowels (in this order). #ANSWER: #PROBLEM 8: # Create the following matrix elegantly: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 1 0 1 0 1 0 1 0 1 [2,] 1 0 1 0 1 0 1 0 1 0 [3,] 0 1 0 1 0 1 0 1 0 1 [4,] 1 0 1 0 1 0 1 0 1 0 [5,] 0 1 0 1 0 1 0 1 0 1 [6,] 1 0 1 0 1 0 1 0 1 0 [7,] 0 1 0 1 0 1 0 1 0 1 [8,] 1 0 1 0 1 0 1 0 1 0 [9,] 0 1 0 1 0 1 0 1 0 1 [10,] 1 0 1 0 1 0 1 0 1 0 #ANSWER: #PROBLEM 9: # Create a 6x3 matrix named 'dat' with # row names "Adam", "Anna", "Bill", "Berta", "Chris", "Cindy". # and column names "Age", "Gender", "Height". # Fill the columns with realistically looking random numbers # so someone could actually believe they are real data. # Use numeric codes 0 and 1 for gender. # Assume the ages to be uniformly distributed between 18 and 24. # Assume men's mean height is 68in and women's 4in less, # but both heights have the same sdev. # Finally assume normal distributions for heights, # except the numbers should be whole inches to look realistic. #ANSWER: #PROBLEM 10: # Create an artificial data matrix with 100000 rows and # columns "Gender", "Age", "Graduated". # Assume P(female)=0.57 (code 1) and P(male)=0.43 (code 0). # Assume Age is uniformly distributed over 18:30. # Assume Graduated is a binary variable with codes 0/1 (not/yes). # Assume further the probabilities of having graduated are # a function of Age as follows: 0,.01,.1,.2,.7,.95,.99,.99,... # for ages 18 and up. # (Hint: To fill 'Graduated', create first a column 'prob' that for # each individual contains the probability of having graduated # dependent on 'Age'. This is the tricky part. # Then set 'Graduated' to runif(100000)