hw2.R

Allison Gonzales — Oct 7, 2013, 3:18 PM

##Allison Gonzales
## Hw2

##Prob 2.1. File created

##Prob 2.2.
## 6 types of objects:
## Atomic (vector, matrix, and array) objects can only contain elements
## from one mode: character, numeric, or logical.
## Vector: collection of elements with dimensions
## Matrix: collection of elements organized into 2-D 
## Array: collection of elements organized into n-D

##Recursive (list, data frame, function) can obtain one or more data objects
## where each object can be of any mode.
## List: collection of data objects without restrictions
## Data frame: list with 2-D structure
## Function: can create, manipulate, operate on, and store data

##Prob 2.3. 
## 3 - We can index objects based on position, name, or logical vector.

##Prob 2.4.
## Replaced

##Prob 2.5.
tab <- matrix(c(139, 443, 230, 502), 2, 2, dimnames = list(VitalStatus = c("Dead", "Alive"), Smoking = c("Yes", "No")))
tab
           Smoking
VitalStatus Yes  No
      Dead  139 230
      Alive 443 502

##Prob 2.6.
rowtotal <- apply(tab, 1, sum)
tab2 <-cbind(tab, Total = rowtotal)
coltotal <- apply(tab, 2, sum)
tab2 <- rbind(tab2, Total = coltotal)
Warning: number of columns of result is not a multiple of vector length
(arg 2)
dimnames(tab2) <- list(VitalStatus = c("Dead", "Alive", "Total"), Smoking = c("Yes", "No", "Total"))
tab2
           Smoking
VitalStatus Yes  No Total
      Dead  139 230   369
      Alive 443 502   945
      Total 582 732   582

##Prob 2.7.
rowdist <- sweep(tab, 1, rowtotal, "/")
rowdist
           Smoking
VitalStatus    Yes     No
      Dead  0.3767 0.6233
      Alive 0.4688 0.5312

coldist <- sweep(tab, 2, coltotal, "/")
coldist
           Smoking
VitalStatus    Yes     No
      Dead  0.2388 0.3142
      Alive 0.7612 0.6858

joindist <- tab/sum(tab)
joindist
           Smoking
VitalStatus    Yes    No
      Dead  0.1058 0.175
      Alive 0.3371 0.382

##Prob 2.8.
risk = tab2[1, 1:2]/tab2[3, 1:2]
RR <- risk/risk[2]
odds <- risk/(1-risk)
OR <- odds/odds[2]
ratios <-rbind(risk, RR, odds, OR)
ratios
        Yes     No
risk 0.2388 0.3142
RR   0.7601 1.0000
odds 0.3138 0.4582
OR   0.6848 1.0000

##Prob 2.9.
wdat = read.table("http://www.medepi.net/data/whickham-engl.txt", sep = ",", header = TRUE) 
str(wdat)
'data.frame':   106 obs. of  1 variable:
 $ X.....DEL: Factor w/ 92 levels "    ","          ",..: 67 67 73 89 88 17 18 19 20 91 ...
xtabs(~Vital.Status + Age + Smoking, data = wdat)
Error: object 'Vital.Status' not found

##Prob 2.10.
std <- read.table("http://www.medepi.net/data/syphilis89c.txt", head = TRUE, sep = ",") 
str(std)
'data.frame':   106 obs. of  1 variable:
 $ X.....DEL: Factor w/ 92 levels "    ","          ",..: 67 67 73 89 88 17 18 19 20 91 ...
table(std$Race, std$Age, std$Sex)
< table of extent 0 x 0 x 0 >
xtabs(~Race + Age + Sex, data = std)
Error: object 'Race' not found

attach(std)
table(Race, Age, Sex)
Error: object 'Race' not found
xtabs(~Race + Age + Sex)
Error: object 'Race' not found

##Prob 2.11
stdarray <-table(std$Age, std$Race, std$Sex)
stdarray
< table of extent 0 x 0 x 0 >
stdage.race <- apply(stdarray, c(1, 2), sum)
stdage.race
<0 x 0 matrix>
stdage.sex <- apply(stdarray, c(1, 3), sum)
stdage.sex
<0 x 0 matrix>
stdrace.age <- apply(stdarray, c(2, 3), sum)
stdrace.age
<0 x 0 matrix>

stdage <- apply(stdarray, 1, sum)
stdage
integer(0)
stdrace <- apply(stdarray, 1, sum)
stdrace
integer(0)
stdsex <- apply(stdarray, 3, sum)
stdsex
integer(0)

stdarray <- table(std$Age, std$Race, std$Sex)
rowtotal <-apply(stdarray, c(1, 3), sum)
rowdist <- sweep(stdarray, c(1, 3), rowtotal, "/")
rowdist

coltotal <- apply(stdarray, c(2, 3), sum)
coldist <- sweep(stdarray, c(2, 3), coltotal, "/")
coldist

joint <- apply(stdarray, 3, sum)
joindist <- sweep(stdarray, 3, joint, "/")
joindist

dist <- list(row.distribution = rowdist, col.distribution = coldist, joint.distribution = joindist)
dist
$row.distribution

$col.distribution

$joint.distribution

##Prob 2.13.
sph <- read.table("http://www.medepi.net/data/syphilis89b.txt", head = TRUE, sep = ",")
sph
                                                                                                                                                                      X.....DEL
1                                                                                                                                                                     <!-- DEL 
2                                                                                                                                                                     <!-- DEL 
3                                                                            <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>
4                                                                                                                                                                        <html>
5                                                                                                                                                                        <head>
6                                                                                                               <meta content=text/html; charset=utf-8 http-equiv=content-type>
7                                                                                                                              <META HTTP-EQUIV=CACHE-CONTROL CONTENT=NO-CACHE>
8                                                                                                                                     <META HTTP-EQUIV=PRAGMA CONTENT=NO-CACHE>
9                                                                                                            <meta name=viewport content=width=device-width, user-scalable=yes>
10                                                                                                                                                                      <title>
11                                                                                                                                                                     AirBears
12                                                                                                                                                                     </title>
13                                                                                                         <link type=text/css rel=stylesheet media=screen href=/css/style.css>
14                                                                                                      <link type=text/css rel=stylesheet media=screen href=/css/ist-main.css>
15                                                                                                      <link type=text/css rel=stylesheet media=screen href=/css/ist-blue.css>
16                                                                     <link type=text/css rel=stylesheet media=only screen and (max-device-width: 480px) href=/css/mobile.css>
17                                                                                                      <link type=text/css rel=stylesheet media=handheld href=/css/mobile.css>
18                                                                                                                                                                      </head>
19                                                                                                                                                                       <body>
20                                                                                                                                                <div id=ucbito_top_container>
21                                                                                                                                         <!-- begin /ssi_top_banner.htmlf -->
22                                                  \t<img src=/img/IST.jpg width=250 alt=UC Berkeley Information Services and Technology border=0 id=ucbito_top_logo height=70>
23                                                                                                                                           <!-- end /ssi_top_banner.htmlf -->
24                                                                                                                                  <!-- begin /ssi_primary_menu_home.htmlf -->
25                                                                                                                                    <!-- end /ssi_primary_menu_home.htmlf -->
26                                                                                                                                                                       </div>
27                                                                                                                                               <div id=ucbito_left_container>
28                                                                                                                             <div class=content><div class=ucbito_left_photo>
29                                                                                                              <img src=/img/airbears_graphic.png hspace=20 alt=AirBears Logo>
30                                                                                                                                                                 </div></div>
31                                                                                                                                                                       </div>
32                                                                                                                                               <div id=ucbito_main_container>
33                                                                                                                                         <div id=ucbito_main_right_container>
34                                                                                                                                                 <div class=ucbito_right_box>
35                                                                             <p> AirBears2 is now available. This network provides simplified authentication for campus users
36   and improved confidentiality for data being transmitted wirelessly. Find more information at <a href=http://airbears.berkeley.edu> https://airbears.berkeley.edu </a> </p>
37                                                                                                                                                                       </div>
38                                                                                                                                                                       </div>
39                                                                                                                                                     <a name=maincontent></a>
40                                                                                                                                           <div id=ucbito_main_mid_container>
41                                                                                                                                                                        <div>
42                                                                                                                                                          <h1> AirBears </h1>
43                                                                                                                                                                        <div>
44                                                                                                                                                       <p>Welcome to AirBears
45                                                                                                                                                                          the
46                                                                                                                                    wireless network at UC Berkeley. AirBears
47                                                                                                                           is operated by Information Services and Technology
48                                                                                                                                                                   (IST).</p>
49                                                                                                                                                           <p>To use AirBears
50                                                                                                                                     you must have a current <b>CalNet</b> ID
51                                                                                                                                                                     (student
52                                                                                                                                                                        staff
53                                                                                                                                                                      faculty
54                                                                                                                                                                or affiliate)
55                                                                                                                                              or a valid <b>Guest Account</b>
56                                                                                                                      obtained from a sponsor. In order to access the network
57                                                                                                                                                                     you must
58                                                                                                                             authenticate using one of the methods below.</p>
59                                                                                                                                                                             
60                                                                                                                                                                      <table>
61                                                                                                                                                                         <tr>
62                                                                                                                                                                        \t<td>
63                                                                                                                                                                             
64                                                                                               \t  <form method=GET action=https://wlan.berkeley.edu/cgi-bin/login/calnet.cgi>
65                                                                                                                                                                          \t  
66                                                                                                                            \t    <input type=submit name=submit value=CalNet>
67                                                                                           \t    <input type=hidden name=url value=http://www.medepi.net/data/syphilis89b.txt>
68                                                                                                                                                                      </form>
69                                                                                                                                                                        </td>
70                                                                                                                                                                        \t<td>
71                                                                                                \t  <form method=GET action=https://wlan.berkeley.edu/cgi-bin/login/guest.cgi>
72                                                                                                                     \t    <input type=submit name=submit value=Guest Account>
73                                                                                                <input type=hidden name=url value=http://www.medepi.net/data/syphilis89b.txt>
74                                                                                                                                                                      </form>
75                                                                                                                                                                        </td>
76                                                                                                                                                                        </tr>
77                                                                                                                                                                    </table> 
78                                                                                                                                                                             
79                                                                                                            <p>Usage of this wireless network denotes acceptance of the terms
80                                                                                           set forth in the <a href=notice.html>AirBears notice</a>. To report a problem with
81                                                                                    AirBears please contact the IST service desk at 510-642-8500 or servicedesk@berkeley.edu.
82                                                                                                                                                                         </p>
83                                                                                                                                                                             
84                                                                                                                                                                          <p>
85                                    The original URL you tried to access is <a href=http://www.medepi.net/data/syphilis89b.txt>http://www.medepi.net/data/syphilis89b.txt</a>
86                                                                                                                                                                         </p>
87                                                                                                                                                                             
88                                                                                                                                                                       </div>
89                                                                                                                                                                       </div>
90                                                                                                                                                                       </div>
91                                                                                                                                                                       </div>
92                                                                                                                                                \t<!-- End of main_content -->
93                                                                                                                                                         <!-- DEL\t</div>  -->
94                                                                                                                                             <div id=ucbito_bottom_container>
95                                                                                                                                       <!-- begin /ssi_context_menu.htmlf -->
96                                                                                                                                                                      \t</div>
97                                                                                                                                         <!-- end /ssi_context_menu.htmlf -->
98                                                                                                                                        <!-- begin /ssi_bottom_menu.htmlf -->
99                                                                                                                                               \t<div class=ucbito_bottom_nav>
100                                                                                  <!-- <span><a href=http://ist.berkeley.edu/accessibility.html>Accessibility</a></span> -->
101                                                                                                                                                                     \t</div>
102                                                                                                                                         <!-- end /ssi_bottom_menu.htmlf -->
103                                                                                                                                                                      </div>
104                                                                                                                                            <div id=ucbito_bottom_bar></div>
105                                                                                                                                                                     </body>
106                                                                                                                                                                     </html>

##Prob 2.14.
#1 Read county names
cty <- scan("http://www.medepi.net/data/calpop/calcounty.txt", what="")

#2 Read county population estimates
calpop = read.csv("http://www.medepi.net/data/calpop/CalCounties2000.txt", header = T)

#2 Replace county number with county name
for(i in 1:length(cty)){calpop$County[calpop$County==i] <- cty[i]}
Error: replacement has 0 rows, data has 106

#3 Discretize age into categories
calpop$Agecat <- cut(calpop$Age, c(0,20,45,65,100),
                     include.lowest = TRUE, right = FALSE)
Error: 'x' must be numeric

#4 Create combined API category
calpop$AsianPI <- calpop$Asian + calpop$Pacific.Islander
Error: replacement has 0 rows, data has 106

#5 Shorten selected ethnic labels
names(calpop)[c(6, 9, 10)] = c("Latino", "AfrAmer", "AmerInd")
Error: 'names' attribute [10] must be the same length as the vector [1]

#6 Index Bay Area Counties
baindex <- calpop$County=="Alameda" | calpop$County=="San Francisco"
bapop <- calpop[baindex,]
bapop
factor(0)
93 Levels:      ... AirBears

#7 Labels for later use
agelabs <- names(table(bapop$Agecat))
Error: $ operator is invalid for atomic vectors
sexlabs <- c("Female", "Male")
racen <- c("White", "AfrAmer", "AsianPI", "Latino", "Multirace",
           "AmerInd")
ctylabs <- names(table(bapop$County))
Error: $ operator is invalid for atomic vectors

#8 Aggregate
bapop2 <- aggregate(bapop[,racen],
                    list(Agecat = bapop$Agecat, Sex = bapop$Sex,
                         County = bapop$County), sum)
Error: incorrect number of dimensions
bapop2
Error: object 'bapop2' not found

#9 Temp matrix of counts
tmp <- as.matrix(cbind(bapop2[1:4,racen], bapop2[5:8,racen],
                       bapop2[9:12,racen], bapop2[13:16,racen]))
Error: object 'bapop2' not found

#10 Convert into final array
bapop3 <- array(tmp, c(4, 6, 2, 2))
Error: object 'tmp' not found
dimnames(bapop3) <- list(agelabs, racen, sexlabs, ctylabs)
Error: object 'agelabs' not found
bapop3
Error: object 'bapop3' not found