getwd()
## [1] "C:/data"
setwd("C:/data")
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
list.files("C:/data")
## [1] "Data1.csv" "Data1.txt" "Data1.xls" "Day1.html"
## [5] "Day1.Rmd" "Day2-2-.html" "Day2-3-.Rmd" "Day2(2).Rmd"
## [9] "Day2(3).Rmd" "Day2.html" "Day2.Rmd" "mtcars.csv"
## [13] "mypcprotector" "period1.xlsx" "rsconnect" "ta1.csv"
library(dplyr)
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df <- read.csv("ta1.csv",fileEncoding = "euc-kr") ### 한글변수명을 읽기 위한 옵션
View(df)
names(df)
## [1] "가해자연령층별.1." "월별.1." "X2022"
## [4] "X2022.1" "X2022.2"
df <- df %>% rename(연령층="가해자연령층별.1.",month="월별.1.",사고건수="X2022",사망자수="X2022.1",부상자수="X2022.2")
names(df)
## [1] "연령층" "month" "사고건수" "사망자수" "부상자수"
View(df)
df[1,]
## 연령층 month 사고건수 사망자수 부상자수
## 1 가해자연령층별(1) 월별(1) 사고건수 (건) 사망자수 (명) 부상자수 (명)
df1 <- df %>% slice(-1,)
View(df1)
dim(df)
## [1] 118 5
dim(df1)
## [1] 117 5
table(df1$month)
##
## 10월 11월 12월 1월 2월 3월 4월 5월 6월 7월 8월 9월 전체
## 9 9 9 9 9 9 9 9 9 9 9 9 9
table(df1$연령층)
##
## 20세이하 21-30세 31-40세 41-50세 51-60세 61-64세 65-70세 71세이상
## 13 13 13 13 13 13 13 13
## 불명
## 13
table(df1$사고건수)
##
## 1018 1042 1074 1107 1112 1211 1234 1257 1296 1306 1324 1395 1443
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1453 1483 1494 15253 1535 1554 1614 1633 1647 1681 1687 1691 1703
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1705 172 1724 1729 1735 1751 1777 1787 1792 183 1855 19160 19399
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1945 1950 1960 1969 2001 211 215 216 2182 2197 2228 224 2268
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2276 2295 2307 231 2310 2322 233 2330 2336 2341 2368 2376 2391
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2409 243 2463 247 2489 2503 2525 2589 259 2682 26904 2714 2760
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2776 280 2805 28402 2849 2900 2930 2939 2994 3044 3083 3096 3191
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 327 33915 357 3677 3683 3733 3747 3772 3872 3911 3989 4001 4156
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 435 44581 473 532 557 580 609 618 639 6508 668 713 845
## 1 1 1 1 1 1 1 1 1 1 1 1 1
table(df1$사망자수)
##
## 0 14 16 17 19 2 20 22 23 24 25 26 27 28 29 297 3 30 31 317
## 13 1 2 2 3 1 2 4 4 1 2 6 4 3 6 1 1 5 3 1
## 32 33 335 34 349 37 38 386 39 40 409 41 42 43 44 47 49 5 50 52
## 3 2 1 5 1 5 3 1 1 1 1 1 2 3 2 2 1 1 1 1
## 53 56 570 6 61 7 72 8
## 1 1 1 4 1 2 1 3
table(df1$부상자수)
##
## 1148 1393 1453 1478 1578 1658 1660 1687 1740 1828 1865 1877 191
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1973 1994 2013 204 2095 21489 2178 2229 2309 231 233 2333 2339
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2386 2396 242 2430 2432 2464 2472 2477 2503 2512 2528 2542 261
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2613 2614 267 2679 275 27566 2769 27792 278 2793 2816 2820 290
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2903 302 305 3079 3086 3209 3285 3302 3382 3418 3419 3428 3442
## 1 1 1 1 1 2 1 1 1 1 1 1 1
## 3443 3446 3460 3470 3534 3546 3579 3613 3615 3719 3750 3878 3896
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 39706 4013 4056 4085 41287 4156 4182 4255 4296 4301 4321 4345 4543
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 48389 486 505 5243 5266 5385 5400 5414 5485 5554 5627 5745 6083
## 1 1 1 1 1 1 1 1 1 1 1 1 1
## 633 63632 678 724 735 805 808 810 847 877 8863 955
## 1 1 1 1 1 1 1 1 1 1 1 1
df2 <- df1 %>% filter(df1$month!="전체")
table(df2$month)
##
## 10월 11월 12월 1월 2월 3월 4월 5월 6월 7월 8월 9월
## 9 9 9 9 9 9 9 9 9 9 9 9
df2 %>% glimpse()
## Rows: 108
## Columns: 5
## $ 연령층 <chr> "20세이하", "20세이하", "20세이하", "20세이하", "20세이하", "…
## $ month <chr> "1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월"…
## $ 사고건수 <chr> "435", "357", "473", "580", "713", "668", "639", "557", "609"…
## $ 사망자수 <chr> "6", "2", "3", "7", "6", "5", "6", "7", "8", "8", "8", "6", "…
## $ 부상자수 <chr> "633", "505", "678", "810", "955", "877", "847", "735", "808"…
df2$사고건수<-as.numeric(df2$사고건수)
df2$사망자수<-as.numeric(df2$사망자수)
df2$부상자수<-as.numeric(df2$부상자수)
df2 %>% glimpse()
## Rows: 108
## Columns: 5
## $ 연령층 <chr> "20세이하", "20세이하", "20세이하", "20세이하", "20세이하", "…
## $ month <chr> "1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월"…
## $ 사고건수 <dbl> 435, 357, 473, 580, 713, 668, 639, 557, 609, 618, 532, 327, 2…
## $ 사망자수 <dbl> 6, 2, 3, 7, 6, 5, 6, 7, 8, 8, 8, 6, 31, 22, 26, 16, 29, 29, 3…
## $ 부상자수 <dbl> 633, 505, 678, 810, 955, 877, 847, 735, 808, 805, 724, 486, 3…
######################################################################################################################
##### 시각화 ggplot2 #####
######################################################################################################################
library(ggplot2)
library(gapminder)
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
y <- gapminder %>% group_by(year,continent) %>% summarize(c_pop=sum(pop))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
y
## # A tibble: 60 × 3
## # Groups: year [12]
## year continent c_pop
## <int> <fct> <dbl>
## 1 1952 Africa 237640501
## 2 1952 Americas 345152446
## 3 1952 Asia 1395357351
## 4 1952 Europe 418120846
## 5 1952 Oceania 10686006
## 6 1957 Africa 264837738
## 7 1957 Americas 386953916
## 8 1957 Asia 1562780599
## 9 1957 Europe 437890351
## 10 1957 Oceania 11941976
## # ℹ 50 more rows
plot(y$year,y$c_pop)
plot(gapminder$gdpPercap,gapminder$lifeExp,col=gapminder$continent)
legend("bottomright",legend = levels(gapminder$continent),pch=c(1:length(levels(gapminder$continent))),col=c(1:1:length(levels(gapminder$continent))))
ggplot(gapminder,aes(x=gdpPercap,y=lifeExp,col=continent,shape=continent))+geom_point()+scale_x_log10()
ggplot(gapminder,aes(x=gdpPercap,y=lifeExp,col=continent,shape=continent,size=pop))+geom_point()+scale_x_log10()
ggplot(gapminder,aes(x=gdpPercap,y=lifeExp,col=continent,shape=continent,size=pop))+geom_point(alpha=0.5)+scale_x_log10() # alpha = 투명도
ggplot(gapminder,aes(x=gdpPercap,y=lifeExp,col=continent,shape=continent,size=pop))+geom_point(alpha=0.5)+scale_x_log10()+facet_wrap(~year) # facet_wrap() = 연도별 데이터 확인 가능
gapminder %>% filter(year==1952&continent=="Asia") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip() # coord_flpi = x축과 y축 바꾸기
gapminder %>% filter(year==1952&continent=="Asia") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")
gapminder %>% filter(year==1952&continent=="Oceania") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip() # coord_flpi = x축과 y축 바꾸기
gapminder %>% filter(year==1952&continent=="Europe") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip()
gapminder %>% filter(year==1952&continent=="Europe") %>% ggplot(aes(country,pop))+geom_bar(stat="identity")+coord_flip()
#######################################################################################################################################################################################
data(cars)
glimpse(cars)
## Rows: 50
## Columns: 2
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34…
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
cars1 <- cars %>% mutate(violation=ifelse(speed>15.4,"fast","slow"))
head(cars1)
## speed dist violation
## 1 4 2 slow
## 2 4 10 slow
## 3 7 4 slow
## 4 7 22 slow
## 5 8 16 slow
## 6 9 10 slow
table(cars1$violation)
##
## fast slow
## 24 26
glimpse(cars1)
## Rows: 50
## Columns: 3
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26…
## $ violation <chr> "slow", "slow", "slow", "slow", "slow", "slow", "slow", "slo…
cars1$violation<-factor(cars1$violation)
glimpse(cars1)
## Rows: 50
## Columns: 3
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26…
## $ violation <fct> slow, slow, slow, slow, slow, slow, slow, slow, slow, slow, …
cars1 %>% group_by(violation) %>% summarize(p=mean(speed)) ### factor=범주형
## # A tibble: 2 × 2
## violation p
## <fct> <dbl>
## 1 fast 19.9
## 2 slow 11.2
library(hflights)
summary(hflights)
## Year Month DayofMonth DayOfWeek DepTime
## Min. :2011 Min. : 1.000 Min. : 1.00 Min. :1.000 Min. : 1
## 1st Qu.:2011 1st Qu.: 4.000 1st Qu.: 8.00 1st Qu.:2.000 1st Qu.:1021
## Median :2011 Median : 7.000 Median :16.00 Median :4.000 Median :1416
## Mean :2011 Mean : 6.514 Mean :15.74 Mean :3.948 Mean :1396
## 3rd Qu.:2011 3rd Qu.: 9.000 3rd Qu.:23.00 3rd Qu.:6.000 3rd Qu.:1801
## Max. :2011 Max. :12.000 Max. :31.00 Max. :7.000 Max. :2400
## NA's :2905
## ArrTime UniqueCarrier FlightNum TailNum
## Min. : 1 Length:227496 Min. : 1 Length:227496
## 1st Qu.:1215 Class :character 1st Qu.: 855 Class :character
## Median :1617 Mode :character Median :1696 Mode :character
## Mean :1578 Mean :1962
## 3rd Qu.:1953 3rd Qu.:2755
## Max. :2400 Max. :7290
## NA's :3066
## ActualElapsedTime AirTime ArrDelay DepDelay
## Min. : 34.0 Min. : 11.0 Min. :-70.000 Min. :-33.000
## 1st Qu.: 77.0 1st Qu.: 58.0 1st Qu.: -8.000 1st Qu.: -3.000
## Median :128.0 Median :107.0 Median : 0.000 Median : 0.000
## Mean :129.3 Mean :108.1 Mean : 7.094 Mean : 9.445
## 3rd Qu.:165.0 3rd Qu.:141.0 3rd Qu.: 11.000 3rd Qu.: 9.000
## Max. :575.0 Max. :549.0 Max. :978.000 Max. :981.000
## NA's :3622 NA's :3622 NA's :3622 NA's :2905
## Origin Dest Distance TaxiIn
## Length:227496 Length:227496 Min. : 79.0 Min. : 1.000
## Class :character Class :character 1st Qu.: 376.0 1st Qu.: 4.000
## Mode :character Mode :character Median : 809.0 Median : 5.000
## Mean : 787.8 Mean : 6.099
## 3rd Qu.:1042.0 3rd Qu.: 7.000
## Max. :3904.0 Max. :165.000
## NA's :3066
## TaxiOut Cancelled CancellationCode Diverted
## Min. : 1.00 Min. :0.00000 Length:227496 Min. :0.000000
## 1st Qu.: 10.00 1st Qu.:0.00000 Class :character 1st Qu.:0.000000
## Median : 14.00 Median :0.00000 Mode :character Median :0.000000
## Mean : 15.09 Mean :0.01307 Mean :0.002853
## 3rd Qu.: 18.00 3rd Qu.:0.00000 3rd Qu.:0.000000
## Max. :163.00 Max. :1.00000 Max. :1.000000
## NA's :2947
glimpse(hflights)
## Rows: 227,496
## Columns: 21
## $ Year <int> 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011…
## $ Month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ DayofMonth <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1…
## $ DayOfWeek <int> 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2…
## $ DepTime <int> 1400, 1401, 1352, 1403, 1405, 1359, 1359, 1355, 1443…
## $ ArrTime <int> 1500, 1501, 1502, 1513, 1507, 1503, 1509, 1454, 1554…
## $ UniqueCarrier <chr> "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA"…
## $ FlightNum <int> 428, 428, 428, 428, 428, 428, 428, 428, 428, 428, 42…
## $ TailNum <chr> "N576AA", "N557AA", "N541AA", "N403AA", "N492AA", "N…
## $ ActualElapsedTime <int> 60, 60, 70, 70, 62, 64, 70, 59, 71, 70, 70, 56, 63, …
## $ AirTime <int> 40, 45, 48, 39, 44, 45, 43, 40, 41, 45, 42, 41, 44, …
## $ ArrDelay <int> -10, -9, -8, 3, -3, -7, -1, -16, 44, 43, 29, 5, -9, …
## $ DepDelay <int> 0, 1, -8, 3, 5, -1, -1, -5, 43, 43, 29, 19, -2, -3, …
## $ Origin <chr> "IAH", "IAH", "IAH", "IAH", "IAH", "IAH", "IAH", "IA…
## $ Dest <chr> "DFW", "DFW", "DFW", "DFW", "DFW", "DFW", "DFW", "DF…
## $ Distance <int> 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 22…
## $ TaxiIn <int> 7, 6, 5, 9, 9, 6, 12, 7, 8, 6, 8, 4, 6, 5, 6, 12, 8,…
## $ TaxiOut <int> 13, 9, 17, 22, 9, 13, 15, 12, 22, 19, 20, 11, 13, 15…
## $ Cancelled <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ CancellationCode <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", …
## $ Diverted <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
hflights %>% count(Month)
## Month n
## 1 1 18910
## 2 2 17128
## 3 3 19470
## 4 4 18593
## 5 5 19172
## 6 6 19600
## 7 7 20548
## 8 8 20176
## 9 9 18065
## 10 10 18696
## 11 11 18021
## 12 12 19117
hflights %>% filter(Month==2|Month==8) %>% dim()
## [1] 37304 21
hflights %>% count(Dest)
## Dest n
## 1 ABQ 2812
## 2 AEX 724
## 3 AGS 1
## 4 AMA 1297
## 5 ANC 125
## 6 ASE 125
## 7 ATL 7886
## 8 AUS 5022
## 9 AVL 350
## 10 BFL 504
## 11 BHM 2736
## 12 BKG 110
## 13 BNA 3481
## 14 BOS 1752
## 15 BPT 3
## 16 BRO 1692
## 17 BTR 1762
## 18 BWI 2551
## 19 CAE 561
## 20 CHS 1200
## 21 CID 410
## 22 CLE 2140
## 23 CLT 4735
## 24 CMH 1348
## 25 COS 1657
## 26 CRP 4813
## 27 CRW 357
## 28 CVG 1535
## 29 DAL 9820
## 30 DAY 451
## 31 DCA 2699
## 32 DEN 5920
## 33 DFW 6653
## 34 DSM 647
## 35 DTW 2601
## 36 ECP 729
## 37 EGE 110
## 38 ELP 3036
## 39 EWR 4314
## 40 FLL 2462
## 41 GJT 403
## 42 GPT 1618
## 43 GRK 42
## 44 GRR 677
## 45 GSO 630
## 46 GSP 1123
## 47 GUC 86
## 48 HDN 110
## 49 HNL 402
## 50 HOB 309
## 51 HRL 3983
## 52 HSV 923
## 53 IAD 1980
## 54 ICT 1517
## 55 IND 1750
## 56 JAN 2011
## 57 JAX 2135
## 58 JFK 695
## 59 LAS 4082
## 60 LAX 6064
## 61 LBB 1333
## 62 LCH 364
## 63 LEX 584
## 64 LFT 2313
## 65 LGA 2730
## 66 LIT 1579
## 67 LRD 1188
## 68 MAF 2306
## 69 MCI 3174
## 70 MCO 3687
## 71 MDW 2094
## 72 MEM 2399
## 73 MFE 1128
## 74 MIA 2463
## 75 MKE 1588
## 76 MLU 292
## 77 MOB 1674
## 78 MSP 2010
## 79 MSY 6823
## 80 MTJ 164
## 81 OAK 690
## 82 OKC 3170
## 83 OMA 2044
## 84 ONT 952
## 85 ORD 5748
## 86 ORF 717
## 87 PBI 1253
## 88 PDX 1235
## 89 PHL 2367
## 90 PHX 5096
## 91 PIT 1664
## 92 PNS 1539
## 93 PSP 106
## 94 RDU 1740
## 95 RIC 900
## 96 RNO 243
## 97 RSW 948
## 98 SAN 2936
## 99 SAT 4893
## 100 SAV 863
## 101 SDF 1279
## 102 SEA 2615
## 103 SFO 2818
## 104 SHV 787
## 105 SJC 885
## 106 SJU 391
## 107 SLC 2033
## 108 SMF 1014
## 109 SNA 1661
## 110 STL 2509
## 111 TPA 3085
## 112 TUL 2924
## 113 TUS 1565
## 114 TYS 1210
## 115 VPS 880
## 116 XNA 1172
hflights %>% count(Dest) %>% arrange(n) %>% head()
## Dest n
## 1 AGS 1
## 2 BPT 3
## 3 GRK 42
## 4 GUC 86
## 5 PSP 106
## 6 BKG 110
hflights %>% count(Dest) %>% arrange(desc(n)) %>% head()
## Dest n
## 1 DAL 9820
## 2 ATL 7886
## 3 MSY 6823
## 4 DFW 6653
## 5 LAX 6064
## 6 DEN 5920
library(gapminder)
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
gapminder %>% count(country)
## # A tibble: 142 × 2
## country n
## <fct> <int>
## 1 Afghanistan 12
## 2 Albania 12
## 3 Algeria 12
## 4 Angola 12
## 5 Argentina 12
## 6 Australia 12
## 7 Austria 12
## 8 Bahrain 12
## 9 Bangladesh 12
## 10 Belgium 12
## # ℹ 132 more rows
gapminder %>% count(continent)
## # A tibble: 5 × 2
## continent n
## <fct> <int>
## 1 Africa 624
## 2 Americas 300
## 3 Asia 396
## 4 Europe 360
## 5 Oceania 24
gapminder %>% group_by(continent) %>% summarize(mean_life=mean(lifeExp)) %>% arrange(mean_life)
## # A tibble: 5 × 2
## continent mean_life
## <fct> <dbl>
## 1 Africa 48.9
## 2 Asia 60.1
## 3 Americas 64.7
## 4 Europe 71.9
## 5 Oceania 74.3
gapminder %>% group_by(continent) %>% summarize(mean_life=mean(lifeExp)) %>% arrange(desc(mean_life))
## # A tibble: 5 × 2
## continent mean_life
## <fct> <dbl>
## 1 Oceania 74.3
## 2 Europe 71.9
## 3 Americas 64.7
## 4 Asia 60.1
## 5 Africa 48.9
tips <- read.csv("http://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv")
glimpse(tips)
## Rows: 244
## Columns: 7
## $ total_bill <dbl> 16.99, 10.34, 21.01, 23.68, 24.59, 25.29, 8.77, 26.88, 15.0…
## $ tip <dbl> 1.01, 1.66, 3.50, 3.31, 3.61, 4.71, 2.00, 3.12, 1.96, 3.23,…
## $ sex <chr> "Female", "Male", "Male", "Male", "Female", "Male", "Male",…
## $ smoker <chr> "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",…
## $ day <chr> "Sun", "Sun", "Sun", "Sun", "Sun", "Sun", "Sun", "Sun", "Su…
## $ time <chr> "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner",…
## $ size <int> 2, 3, 3, 2, 4, 4, 2, 4, 2, 2, 2, 4, 2, 4, 2, 2, 3, 3, 3, 3,…
summary(tips)
## total_bill tip sex smoker
## Min. : 3.07 Min. : 1.000 Length:244 Length:244
## 1st Qu.:13.35 1st Qu.: 2.000 Class :character Class :character
## Median :17.80 Median : 2.900 Mode :character Mode :character
## Mean :19.79 Mean : 2.998
## 3rd Qu.:24.13 3rd Qu.: 3.562
## Max. :50.81 Max. :10.000
## day time size
## Length:244 Length:244 Min. :1.00
## Class :character Class :character 1st Qu.:2.00
## Mode :character Mode :character Median :2.00
## Mean :2.57
## 3rd Qu.:3.00
## Max. :6.00
head(tips,3)
## total_bill tip sex smoker day time size
## 1 16.99 1.01 Female No Sun Dinner 2
## 2 10.34 1.66 Male No Sun Dinner 3
## 3 21.01 3.50 Male No Sun Dinner 3
tips %>% ggplot(aes(size))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
tips %>% ggplot(aes(total_bill,tip))+geom_point()
tips %>% ggplot(aes(total_bill,tip))+geom_point(aes(col=day))
tips %>% ggplot(aes(total_bill,tip,col=day))+geom_point()
tips %>% ggplot(aes(total_bill,tip))+geom_point(aes(col=day,pch=sex),size=2)
tips %>% ggplot(aes(total_bill,tip))+geom_point(aes(col=day,pch=sex),size=2)+facet_wrap(~day)
gapminder %>% ggplot(aes(year,lifeExp,col=continent))+geom_point(alpha=0.2)
gapminder %>% ggplot(aes(year,lifeExp,col=continent))+geom_point(alpha=0.2)+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
x <- gapminder %>% filter(year==1952)
hist(x$lifeExp,main="Histogram of lifeExp in 1952")
x %>% ggplot(aes(continent,lifeExp))+geom_boxplot()
gapminder %>% filter(lifeExp<=45 & continent=="Europe")
## # A tibble: 1 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Turkey Europe 1952 43.6 22235677 1969.
######################################################################################################################
##### regression #####
######################################################################################################################
data(cars)
glimpse(cars)
## Rows: 50
## Columns: 2
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34…
reg1 <- lm(dist~speed,data=cars)
coef(reg1)
## (Intercept) speed
## -17.579095 3.932409
plot(cars)
abline(reg1,col="red")
fitted(reg1)
## 1 2 3 4 5 6 7 8
## -1.849460 -1.849460 9.947766 9.947766 13.880175 17.812584 21.744993 21.744993
## 9 10 11 12 13 14 15 16
## 21.744993 25.677401 25.677401 29.609810 29.609810 29.609810 29.609810 33.542219
## 17 18 19 20 21 22 23 24
## 33.542219 33.542219 33.542219 37.474628 37.474628 37.474628 37.474628 41.407036
## 25 26 27 28 29 30 31 32
## 41.407036 41.407036 45.339445 45.339445 49.271854 49.271854 49.271854 53.204263
## 33 34 35 36 37 38 39 40
## 53.204263 53.204263 53.204263 57.136672 57.136672 57.136672 61.069080 61.069080
## 41 42 43 44 45 46 47 48
## 61.069080 61.069080 61.069080 68.933898 72.866307 76.798715 76.798715 76.798715
## 49 50
## 76.798715 80.731124
residuals(reg1)
## 1 2 3 4 5 6 7
## 3.849460 11.849460 -5.947766 12.052234 2.119825 -7.812584 -3.744993
## 8 9 10 11 12 13 14
## 4.255007 12.255007 -8.677401 2.322599 -15.609810 -9.609810 -5.609810
## 15 16 17 18 19 20 21
## -1.609810 -7.542219 0.457781 0.457781 12.457781 -11.474628 -1.474628
## 22 23 24 25 26 27 28
## 22.525372 42.525372 -21.407036 -15.407036 12.592964 -13.339445 -5.339445
## 29 30 31 32 33 34 35
## -17.271854 -9.271854 0.728146 -11.204263 2.795737 22.795737 30.795737
## 36 37 38 39 40 41 42
## -21.136672 -11.136672 10.863328 -29.069080 -13.069080 -9.069080 -5.069080
## 43 44 45 46 47 48 49
## 2.930920 -2.933898 -18.866307 -6.798715 15.201285 16.201285 43.201285
## 50
## 4.268876
nx1 <- data.frame(speed=c(21.5))
predict(reg1,nx1)
## 1
## 66.96769
nx <- data.frame(speed=c(21.5,25.0,25.5,26.0,26.5,27.0,28.0))
plot(nx$speed,predict(reg1,nx),col="red",cex=2,pch=20)
abline(reg1)
data(mtcars)
glimpse(mtcars)
## Rows: 32
## Columns: 11
## $ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
## $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
## $ hp <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
## $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
## $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
## $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…
View(mtcars)
reg_mtcars <- lm(qsec~.,data=mtcars)
coef(reg_mtcars)
## (Intercept) mpg cyl disp hp drat
## 17.776176930 0.069047681 -0.362678233 -0.007500838 -0.001562552 -0.131063599
## wt vs am gear carb
## 1.496332238 0.970034739 -0.901186410 -0.201285113 -0.273597512
fitted(reg_mtcars)
## Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive
## 16.28662 16.66818 18.90135 19.47025
## Hornet Sportabout Valiant Duster 360 Merc 240D
## 16.77568 19.90627 16.00195 20.72035
## Merc 230 Merc 280 Merc 280C Merc 450SE
## 20.51257 19.18061 19.08394 17.92020
## Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
## 17.47359 17.40341 17.50561 17.83118
## Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla
## 18.10631 19.61647 18.26239 19.21410
## Toyota Corona Dodge Challenger AMC Javelin Camaro Z28
## 20.05368 17.07964 16.98564 16.34377
## Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa
## 17.12536 18.86554 17.24289 17.81968
## Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E
## 14.10868 15.72047 13.91081 19.06281
residuals(reg_mtcars)
## Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive
## 0.17338046 0.35181574 -0.29134957 -0.03024515
## Hornet Sportabout Valiant Duster 360 Merc 240D
## 0.24432336 0.31373004 -0.16195252 -0.72035069
## Merc 230 Merc 280 Merc 280C Merc 450SE
## 2.38743281 -0.88060648 -0.18393973 -0.52020163
## Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
## 0.12640842 0.59659193 0.47438919 -0.01118269
## Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla
## -0.68631131 -0.14646627 0.25760677 0.68590346
## Toyota Corona Dodge Challenger AMC Javelin Camaro Z28
## -0.04367648 -0.20964444 0.31436118 -0.93376985
## Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa
## -0.07535598 0.03445520 -0.54289290 -0.91968405
## Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E
## 0.39132068 -0.22047172 0.68919207 -0.46280987
plot(reg_mtcars)
######################################################################################################################
##### machine learning #####
######################################################################################################################
library(caret)
## 필요한 패키지를 로딩중입니다: lattice
library(mlbench)
data(Sonar)
glimpse(Sonar)
## Rows: 208
## Columns: 61
## $ V1 <dbl> 0.0200, 0.0453, 0.0262, 0.0100, 0.0762, 0.0286, 0.0317, 0.0519, …
## $ V2 <dbl> 0.0371, 0.0523, 0.0582, 0.0171, 0.0666, 0.0453, 0.0956, 0.0548, …
## $ V3 <dbl> 0.0428, 0.0843, 0.1099, 0.0623, 0.0481, 0.0277, 0.1321, 0.0842, …
## $ V4 <dbl> 0.0207, 0.0689, 0.1083, 0.0205, 0.0394, 0.0174, 0.1408, 0.0319, …
## $ V5 <dbl> 0.0954, 0.1183, 0.0974, 0.0205, 0.0590, 0.0384, 0.1674, 0.1158, …
## $ V6 <dbl> 0.0986, 0.2583, 0.2280, 0.0368, 0.0649, 0.0990, 0.1710, 0.0922, …
## $ V7 <dbl> 0.1539, 0.2156, 0.2431, 0.1098, 0.1209, 0.1201, 0.0731, 0.1027, …
## $ V8 <dbl> 0.1601, 0.3481, 0.3771, 0.1276, 0.2467, 0.1833, 0.1401, 0.0613, …
## $ V9 <dbl> 0.3109, 0.3337, 0.5598, 0.0598, 0.3564, 0.2105, 0.2083, 0.1465, …
## $ V10 <dbl> 0.2111, 0.2872, 0.6194, 0.1264, 0.4459, 0.3039, 0.3513, 0.2838, …
## $ V11 <dbl> 0.1609, 0.4918, 0.6333, 0.0881, 0.4152, 0.2988, 0.1786, 0.2802, …
## $ V12 <dbl> 0.1582, 0.6552, 0.7060, 0.1992, 0.3952, 0.4250, 0.0658, 0.3086, …
## $ V13 <dbl> 0.2238, 0.6919, 0.5544, 0.0184, 0.4256, 0.6343, 0.0513, 0.2657, …
## $ V14 <dbl> 0.0645, 0.7797, 0.5320, 0.2261, 0.4135, 0.8198, 0.3752, 0.3801, …
## $ V15 <dbl> 0.0660, 0.7464, 0.6479, 0.1729, 0.4528, 1.0000, 0.5419, 0.5626, …
## $ V16 <dbl> 0.2273, 0.9444, 0.6931, 0.2131, 0.5326, 0.9988, 0.5440, 0.4376, …
## $ V17 <dbl> 0.3100, 1.0000, 0.6759, 0.0693, 0.7306, 0.9508, 0.5150, 0.2617, …
## $ V18 <dbl> 0.2999, 0.8874, 0.7551, 0.2281, 0.6193, 0.9025, 0.4262, 0.1199, …
## $ V19 <dbl> 0.5078, 0.8024, 0.8929, 0.4060, 0.2032, 0.7234, 0.2024, 0.6676, …
## $ V20 <dbl> 0.4797, 0.7818, 0.8619, 0.3973, 0.4636, 0.5122, 0.4233, 0.9402, …
## $ V21 <dbl> 0.5783, 0.5212, 0.7974, 0.2741, 0.4148, 0.2074, 0.7723, 0.7832, …
## $ V22 <dbl> 0.5071, 0.4052, 0.6737, 0.3690, 0.4292, 0.3985, 0.9735, 0.5352, …
## $ V23 <dbl> 0.4328, 0.3957, 0.4293, 0.5556, 0.5730, 0.5890, 0.9390, 0.6809, …
## $ V24 <dbl> 0.5550, 0.3914, 0.3648, 0.4846, 0.5399, 0.2872, 0.5559, 0.9174, …
## $ V25 <dbl> 0.6711, 0.3250, 0.5331, 0.3140, 0.3161, 0.2043, 0.5268, 0.7613, …
## $ V26 <dbl> 0.6415, 0.3200, 0.2413, 0.5334, 0.2285, 0.5782, 0.6826, 0.8220, …
## $ V27 <dbl> 0.7104, 0.3271, 0.5070, 0.5256, 0.6995, 0.5389, 0.5713, 0.8872, …
## $ V28 <dbl> 0.8080, 0.2767, 0.8533, 0.2520, 1.0000, 0.3750, 0.5429, 0.6091, …
## $ V29 <dbl> 0.6791, 0.4423, 0.6036, 0.2090, 0.7262, 0.3411, 0.2177, 0.2967, …
## $ V30 <dbl> 0.3857, 0.2028, 0.8514, 0.3559, 0.4724, 0.5067, 0.2149, 0.1103, …
## $ V31 <dbl> 0.1307, 0.3788, 0.8512, 0.6260, 0.5103, 0.5580, 0.5811, 0.1318, …
## $ V32 <dbl> 0.2604, 0.2947, 0.5045, 0.7340, 0.5459, 0.4778, 0.6323, 0.0624, …
## $ V33 <dbl> 0.5121, 0.1984, 0.1862, 0.6120, 0.2881, 0.3299, 0.2965, 0.0990, …
## $ V34 <dbl> 0.7547, 0.2341, 0.2709, 0.3497, 0.0981, 0.2198, 0.1873, 0.4006, …
## $ V35 <dbl> 0.8537, 0.1306, 0.4232, 0.3953, 0.1951, 0.1407, 0.2969, 0.3666, …
## $ V36 <dbl> 0.8507, 0.4182, 0.3043, 0.3012, 0.4181, 0.2856, 0.5163, 0.1050, …
## $ V37 <dbl> 0.6692, 0.3835, 0.6116, 0.5408, 0.4604, 0.3807, 0.6153, 0.1915, …
## $ V38 <dbl> 0.6097, 0.1057, 0.6756, 0.8814, 0.3217, 0.4158, 0.4283, 0.3930, …
## $ V39 <dbl> 0.4943, 0.1840, 0.5375, 0.9857, 0.2828, 0.4054, 0.5479, 0.4288, …
## $ V40 <dbl> 0.2744, 0.1970, 0.4719, 0.9167, 0.2430, 0.3296, 0.6133, 0.2546, …
## $ V41 <dbl> 0.0510, 0.1674, 0.4647, 0.6121, 0.1979, 0.2707, 0.5017, 0.1151, …
## $ V42 <dbl> 0.2834, 0.0583, 0.2587, 0.5006, 0.2444, 0.2650, 0.2377, 0.2196, …
## $ V43 <dbl> 0.2825, 0.1401, 0.2129, 0.3210, 0.1847, 0.0723, 0.1957, 0.1879, …
## $ V44 <dbl> 0.4256, 0.1628, 0.2222, 0.3202, 0.0841, 0.1238, 0.1749, 0.1437, …
## $ V45 <dbl> 0.2641, 0.0621, 0.2111, 0.4295, 0.0692, 0.1192, 0.1304, 0.2146, …
## $ V46 <dbl> 0.1386, 0.0203, 0.0176, 0.3654, 0.0528, 0.1089, 0.0597, 0.2360, …
## $ V47 <dbl> 0.1051, 0.0530, 0.1348, 0.2655, 0.0357, 0.0623, 0.1124, 0.1125, …
## $ V48 <dbl> 0.1343, 0.0742, 0.0744, 0.1576, 0.0085, 0.0494, 0.1047, 0.0254, …
## $ V49 <dbl> 0.0383, 0.0409, 0.0130, 0.0681, 0.0230, 0.0264, 0.0507, 0.0285, …
## $ V50 <dbl> 0.0324, 0.0061, 0.0106, 0.0294, 0.0046, 0.0081, 0.0159, 0.0178, …
## $ V51 <dbl> 0.0232, 0.0125, 0.0033, 0.0241, 0.0156, 0.0104, 0.0195, 0.0052, …
## $ V52 <dbl> 0.0027, 0.0084, 0.0232, 0.0121, 0.0031, 0.0045, 0.0201, 0.0081, …
## $ V53 <dbl> 0.0065, 0.0089, 0.0166, 0.0036, 0.0054, 0.0014, 0.0248, 0.0120, …
## $ V54 <dbl> 0.0159, 0.0048, 0.0095, 0.0150, 0.0105, 0.0038, 0.0131, 0.0045, …
## $ V55 <dbl> 0.0072, 0.0094, 0.0180, 0.0085, 0.0110, 0.0013, 0.0070, 0.0121, …
## $ V56 <dbl> 0.0167, 0.0191, 0.0244, 0.0073, 0.0015, 0.0089, 0.0138, 0.0097, …
## $ V57 <dbl> 0.0180, 0.0140, 0.0316, 0.0050, 0.0072, 0.0057, 0.0092, 0.0085, …
## $ V58 <dbl> 0.0084, 0.0049, 0.0164, 0.0044, 0.0048, 0.0027, 0.0143, 0.0047, …
## $ V59 <dbl> 0.0090, 0.0052, 0.0095, 0.0040, 0.0107, 0.0051, 0.0036, 0.0048, …
## $ V60 <dbl> 0.0032, 0.0044, 0.0078, 0.0117, 0.0094, 0.0062, 0.0103, 0.0053, …
## $ Class <fct> R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R…
Sonar %>% count(Class)
## Class n
## 1 M 111
## 2 R 97
set.seed(998)
inTraining <- createDataPartition(Sonar$Class,p=.75,list=FALSE)
training <- Sonar[inTraining,]
testing <- Sonar[-inTraining,]
NROW(training)
## [1] 157
NROW(testing)
## [1] 51
157/208*100
## [1] 75.48077
data(gapminder)
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
gapminder %>% count(continent)
## # A tibble: 5 × 2
## continent n
## <fct> <int>
## 1 Africa 624
## 2 Americas 300
## 3 Asia 396
## 4 Europe 360
## 5 Oceania 24
set.seed(150000)
inTraining <- createDataPartition(gapminder$continent,p=0.7,list=FALSE)
training <- gapminder[inTraining,]
testing <- gapminder[-inTraining,]
a <- NROW(training)
b <- NROW(testing)
a/(a+b)*100
## [1] 70.07042
data(diamonds)
glimpse(diamonds)
## Rows: 53,940
## Columns: 10
## $ carat <dbl> 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, 0.23, 0.…
## $ cut <ord> Ideal, Premium, Good, Premium, Good, Very Good, Very Good, Ver…
## $ color <ord> E, E, E, I, J, J, I, H, E, H, J, J, F, J, E, E, I, J, J, J, I,…
## $ clarity <ord> SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI1, VS1, …
## $ depth <dbl> 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, 59.4, 64…
## $ table <dbl> 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54, 62, 58…
## $ price <int> 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339, 340, 34…
## $ x <dbl> 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, 4.00, 4.…
## $ y <dbl> 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, 4.05, 4.…
## $ z <dbl> 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, 2.39, 2.…
set.seed(70)
inTraining <- createDataPartition(diamonds$cut,p=.6,list=FALSE)
training <- diamonds[inTraining,]
testing <- diamonds[-inTraining,]
a <- NROW(training)
b <- NROW(testing)
a/(a+b)*100
## [1] 60.00371
######################################################################################################################
##### logistic regression #####
######################################################################################################################
ucla <- read.csv("http://stats.idre.ucla.edu/stat/data/binary.csv")
glimpse(ucla)
## Rows: 400
## Columns: 4
## $ admit <int> 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1…
## $ gre <int> 380, 660, 800, 640, 520, 760, 560, 400, 540, 700, 800, 440, 760,…
## $ gpa <dbl> 3.61, 3.67, 4.00, 3.19, 2.93, 3.00, 2.98, 3.08, 3.39, 3.92, 4.00…
## $ rank <int> 3, 3, 1, 4, 4, 2, 1, 2, 3, 2, 4, 1, 1, 2, 1, 3, 4, 3, 2, 1, 3, 2…
ucla$admit <- as.factor(ucla$admit)
glimpse(ucla)
## Rows: 400
## Columns: 4
## $ admit <fct> 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1…
## $ gre <int> 380, 660, 800, 640, 520, 760, 560, 400, 540, 700, 800, 440, 760,…
## $ gpa <dbl> 3.61, 3.67, 4.00, 3.19, 2.93, 3.00, 2.98, 3.08, 3.39, 3.92, 4.00…
## $ rank <int> 3, 3, 1, 4, 4, 2, 1, 2, 3, 2, 4, 1, 1, 2, 1, 3, 4, 3, 2, 1, 3, 2…
logit <- glm(admit~.,data=ucla,family = "binomial")
summary(logit)
##
## Call:
## glm(formula = admit ~ ., family = "binomial", data = ucla)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -3.449548 1.132846 -3.045 0.00233 **
## gre 0.002294 0.001092 2.101 0.03564 *
## gpa 0.777014 0.327484 2.373 0.01766 *
## rank -0.560031 0.127137 -4.405 1.06e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 499.98 on 399 degrees of freedom
## Residual deviance: 459.44 on 396 degrees of freedom
## AIC: 467.44
##
## Number of Fisher Scoring iterations: 4
exp(0.777014)
## [1] 2.174968
exp(-0.560031)
## [1] 0.5711914
s3 <- data.frame(gre=c(376),gpa=c(3.6),rank=c(3))
predict(logit,newdata=s3,type="response")
## 1
## 0.1869631
s1 <- data.frame(gre=c(376),gpa=c(3.6),rank=c(1))
predict(logit,newdata=s1,type="response")
## 1
## 0.4134304
ss <- data.frame(gre=c(390),gpa=c(3.6),rank=c(1))
predict(logit,newdata=ss,type="response")
## 1
## 0.4212396
sss <- data.frame(gre=c(500),gpa=c(3.6),rank=c(1))
predict(logit,newdata=sss,type="response")
## 1
## 0.483668
dim(ucla)
## [1] 400 4
######################################################################################################################
featurePlot(x=iris[,1:4],
y=iris$Species,
plot="density",
scales=list(x=list(relation="free"),y=list(relation="free")),adjust=1.5,pch="|",layout=c(4,1),auto.key=list(colSums=3))
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.