'STR'에 해당되는 글 1건

  1. 2015.10.29 dim, str, plot
반응형

source: https://class.coursera.org/statistics-004 (coursera: Data Analysis and Statistical Inference by Duke Univ. by Dr. Mine Çetinkaya-Rundel)

 

The present data set refers to the number of male and female births in the United States. The data set contains the data for all years from 1940 to 2002. We can take a look at the data by typing its name into the console.

> source("http://www.openintro.org/stat/data/present.R")
> head(present)
  year    boys   girls
1 1940 1211684 1148715
2 1941 1289734 1223693
3 1942 1444365 1364631
4 1943 1508959 1427901
5 1944 1435301 1359499
6 1945 1404587 


You can see the dimensions of this data frame by typing:

> dim(present)
[1] 63  3
> names(present)
[1] "year"  "boys"  "girls"
> str(present)
'data.frame':   63 obs. of  3 variables:
 $ year : num  1940 1941 1942 1943 1944 ...
 $ boys : num  1211684 1289734 1444365 1508959 1435301 ...
 $ girls: num  1148715 1223693 1364631 1427901 1359499 ...

 

Let’s start to examine the data a little more closely. We can access the data in a single column of a data frame separately using a command like

> present$boys
 [1] 1211684 1289734 1444365 1508959 1435301 1404587 1691220 1899876 1813852 1826352 1823555 1923020 1971262 2001798
[15] 2059068 2073719 2133588 2179960 2152546 2173638 2179708 2186274 2132466 2101632 2060162 1927054 1845862 1803388
[29] 1796326 1846572 1915378 1822910 1669927 1608326 1622114 1613135 1624436 1705916 1709394 1791267 1852616 1860272
[43] 1885676 1865553 1879490 1927983 1924868 1951153 2002424 2069490 2129495 2101518 2082097 2048861 2022589 1996355
[57] 1990480 1985596 2016205 2026854 2076969 2057922 2057979

 

We can create a simple plot of the number of girls born per year with the command

> plot(x = present$year, y = present$girls)

-> There is initially an increase in the number of girls born, which peaks around 1960. After 1960 there is a decrease in the number of girls born, but the number begins to increase again in the early 1970s. Overall the trend is an increase in the number of girls born in the US since the 1940s.


If we wanted to connect the data points with lines, we could add a third argument, the letter l for line.

> plot(x = present$year, y = present$girls,type="l")

 

If we wanted to make the title,

> plot(x = present$year, y = present$girls,main="Girls born per year")

 


to see the total number of births in 1940, we add the vector for births for boys and girls, R will compute all sums simultaneously.

> present$boys+present$girls
 [1] 2360399 2513427 2808996 2936860 2794800 2735456 3288672 3699940 3535068
[10] 3559529 3554149 3750850 3846986 3902120 4017362 4047295 4163090 4254784
[19] 4203812 4244796 4257850 4268326 4167362 4098020 4027490 3760358 3606274
[28] 3520959 3501564 3600206 3731386 3555970 3258411 3136965 3159958 3144198
[37] 3167788 3326632 3333279 3494398 3612258 3629238 3680537 3638933 3669141
[46] 3760561 3756547 3809394 3909510 4040958 4158212 4110907 4065014 4000240
[55] 3952767 3899589 3891494 3880894 3941553 3959417 4058814 4025933 4021726

 

we can make a plot of the total number of births per year with the command

> plot(present$year,present$boys+present$girls,type="l")

  

-> In 1961, we can see the most total number of births in the U.S.


The proportion of newborns that are boys

> present$boys/(present$boys+present$girls)
 [1] 0.5133386 0.5131376 0.5141926 0.5138001 0.5135613 0.5134745 0.5142562
 [8] 0.5134883 0.5131024 0.5130881 0.5130778 0.5126891 0.5124173 0.5130027
[15] 0.5125423 0.5123716 0.5125011 0.5123550 0.5120462 0.5120713 0.5119269
[22] 0.5122088 0.5117064 0.5128408 0.5115250 0.5124656 0.5118474 0.5121866
[29] 0.5130068 0.5129073 0.5133154 0.5126337 0.5124973 0.5127013 0.5133340
[36] 0.5130513 0.5127982 0.5128057 0.5128266 0.5126110 0.5128692 0.5125792
[43] 0.5123372 0.5126648 0.5122425 0.5126849 0.5124035 0.5121951 0.5121931
[50] 0.5121286 0.5121179 0.5112054 0.5121992 0.5121845 0.5116894 0.5119398
[57] 0.5114951 0.5116337 0.5115255 0.5119072 0.5117182 0.5111665 0.5117154

 -> Based on the plot determine, the proportion of boys born in the US has decreased over time and every year there are more boys born than girls.


The present data set refers to the number of male and female births in the United States. The data set contains the data for all years from 1940 to 2002. We can take a look at the data by typing its name into the console.

> plot(present$year, present$boys/(present$boys+present$girls))

 

 We can ask if boys outnumber girls in each year with the expression

> present$boys > present$girls
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[15] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[29] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[43] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[57] TRUE TRUE TRUE TRUE TRUE TRUE TRUE

-> Every year there are more boys born than girls.

 

plot(present$year,present$boys-present$girls)

 

 If we cant’ see the exact number of y-axis

> dd<-present$boys-present$girls
> min(dd,na.rm=T)
[1] 62969
> max(dd,na.rm=T)
[1] 105244
> plot(present$year,dd,ylim=c(60000,120000))

  

 

If we wanted to name the y-axis and the title,

> plot(present$year,dd,ylab="differences number of boys and girls", 
ylim=c(60000,120000),main="Absolute differences")


 

-> In 1963, we can see the biggest absolute differences number of boys and girls born. There is initially a decrease in the boy-to-girl ratio, and then an increase between 1960 and 1970, followed by a decrease.


X, y축의 평균을 표기 하기 위해서는

> plot(present$boys,present$girls,xlim=c(1100000,2000000),
ylim=c(1100000,2200000))
> abline(v=mean(present$boys),lty=2) 
> abline(h=mean(present$girls),lty=2)




반응형
Posted by 마르띤
,