library(caret)


createDataPartition()


: 층화 균등 추출에 사용되는 함수이다. 계층 별로 비율을 동일하게, 랜덤하게 뽑아준다.





   1) createDataPartition(y, times = 1, p = 0.5, list = TRUE, groups = min(5, length(y)))


      - y : 대상 vector


      - p : 선택할 데이터 확률


   2) 사용예


      - df <- data.frame(replicate(10,sample(1:3, 20,rep=TRUE)))


      - createDataPartition(y = df$X1, p = 0.7, list = FALSE, groups = min(2, length(df$X1)))





[출처] R 표본 샘플링 - 홀드아웃 holdout 방법|작성자 나리

https://blog.naver.com/nyaminyam/221246413590



[출처] sample, createDataPartition 사용법|작성자 이해할때까지

https://blog.naver.com/wujuchoi/221058021095

'programing > R studio' 카테고리의 다른 글

기하분포 - dgeom  (0) 2018.07.02
ggplot2 - scale_x_discrete, scale_y_discrete - x, y값 순서 정렬  (0) 2018.06.29
dcast()  (0) 2018.06.26
정규화란?  (0) 2018.06.25
선형회귀 lm(), predict(), abline(), coef()  (0) 2018.06.24
> library(reshape2)
> 
> fm<-melt(id=1:4, french_fries)
> head(fm)
  time treatment subject rep variable value
1    1         1       3   1   potato   2.9
2    1         1       3   2   potato  14.0
3    1         1      10   1   potato  11.0
4    1         1      10   2   potato   9.9
5    1         1      15   1   potato   1.2
6    1         1      15   2   potato   8.8
> 
> # dcast() melt  상태로 돌려주는 !
> # ...  => 나열 되지 않은 나머지 모든 변수
> x<-dcast(fm, time+treatment+subject+rep~variable)
> head(x)
  time treatment subject rep potato buttery grassy rancid painty
1    1         1       3   1    2.9     0.0    0.0    0.0    5.5
2    1         1       3   2   14.0     0.0    0.0    1.1    0.0
3    1         1      10   1   11.0     6.4    0.0    0.0    0.0
4    1         1      10   2    9.9     5.9    2.9    2.2    0.0
5    1         1      15   1    1.2     0.1    0.0    1.1    5.1
6    1         1      15   2    8.8     3.0    3.6    1.5    2.3
> 
> x<-dcast(fm, time+treatment+subject+rep~...)
> head(x)
  time treatment subject rep potato buttery grassy rancid painty
1    1         1       3   1    2.9     0.0    0.0    0.0    5.5
2    1         1       3   2   14.0     0.0    0.0    1.1    0.0
3    1         1      10   1   11.0     6.4    0.0    0.0    0.0
4    1         1      10   2    9.9     5.9    2.9    2.2    0.0
5    1         1      15   1    1.2     0.1    0.0    1.1    5.1
6    1         1      15   2    8.8     3.0    3.6    1.5    2.3
> 
> 
> head(fm)
  time treatment subject rep variable value
1    1         1       3   1   potato   2.9
2    1         1       3   2   potato  14.0
3    1         1      10   1   potato  11.0
4    1         1      10   2   potato   9.9
5    1         1      15   1   potato   1.2
6    1         1      15   2   potato   8.8
> dcast(fm, time~variable) # 1행만 id 지정; 72 행의 
Aggregation function missing: defaulting to length
   time potato buttery grassy rancid painty
1     1     72      72     72     72     72
2     2     72      72     72     72     72
3     3     72      72     72     72     72
4     4     72      72     72     72     72
5     5     72      72     72     72     72
6     6     72      72     72     72     72
7     7     72      72     72     72     72
8     8     72      72     72     72     72
9     9     60      60     60     60     60
10   10     60      60     60     60     60
> 
> 
> nrow(filter(fm, time==1 & variable == "potato"))
[1] 72

 

library(reshape2)


fm<-melt(id=1:4, french_fries)

head(fm)


# dcast()는 melt 전 상태로 돌려주는 듯!

# ...  => 나열 되지 않은 나머지 모든 변수

x<-dcast(fm, time+treatment+subject+rep~variable)

head(x)


x<-dcast(fm, time+treatment+subject+rep~...)

head(x)



head(fm)

dcast(fm, time~variable) # 1행만 id 지정; 72는 행의 수



nrow(filter(fm, time==1 & variable == "potato"))



데이터의 범위를 일치시키거나 분포를 유사하게 만들어 주는 등의 작업

> 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 

> str(cars)

'data.frame':      50 obs. of  2 variables:

 $ speed: num  4 4 7 7 8 9 10 10 10 11 ...

 $ dist : num  2 10 4 22 16 10 18 26 34 17 ...

> head(cars)

  speed dist

1     4    2

2     4   10

3     7    4

4     7   22

5     8   16

6     9   10

> lm(speed~dist, cars)

 

Call:

lm(formula = speed ~ dist, data = cars)

 

Coefficients:

(Intercept)         dist 

     8.2839       0.1656 

 

>

> m<-lm(dist~speed, cars)

> summary(m)

 

Call:

lm(formula = dist ~ speed, data = cars)

 

Residuals:

    Min      1Q  Median      3Q     Max

-29.069  -9.525  -2.272   9.215  43.201

 

Coefficients:

            Estimate Std. Error t value Pr(>|t|)   

(Intercept) -17.5791     6.7584  -2.601   0.0123 * 

speed         3.9324     0.4155   9.464 1.49e-12 ***

---

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

 

Residual standard error: 15.38 on 48 degrees of freedom

Multiple R-squared:  0.6511,    Adjusted R-squared:  0.6438

F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12

 

>

> # Intercept = 절편

> # coefficients(회귀식의 계수) 종속변수 = 독립변수*계수 + 절편

> # dist=speed*3.932-17.579

> # 계수가 양수이므로 양의 상관관계를 지님

>

>

>

> # 추정해보기(신뢰구간)

>

> predict(m, data.frame(speed=3), interval="confidence")

        fit       lwr      upr

1 -5.781869 -17.02659 5.462853

>

> # fit = 예측값

> # lwr, upr = 신뢰구간

> # speed 3 , dist 평균값은 -17.03~5.46 사이에 있음

>

>

> plot(cars)

> abline(coef(m)) #coef 회선의 계수

> coef(m)

(Intercept)       speed

 -17.579095    3.932409

>

>

> [출처] [R프로그래밍] RStudio 선형 회귀 (1) 단순 선형 회귀|작성자 아트 

http://chloe-ynlee.me/221302458526

 




####################################################

cars

summary(cars)

str(cars)

head(cars)

lm(speed~dist, cars)


m<-lm(dist~speed, cars)

summary(m)


# Intercept = 절편

# coefficients(회귀식의 계수) 종속변수 = 독립변수*계수 + 절편

# dist=speed*3.932-17.579

# 계수가 양수이므로 양의 상관관계를 지님




# 추정해보기(신뢰구간)


predict(m, data.frame(speed=3), interval="confidence")


# fit = 예측값

# lwr, upr = 신뢰구간

# speed가 3일 때, dist의 평균값은 -17.03~5.46 사이에 있음



plot(cars)

abline(coef(m)) #coef는 회선의 계수

coef(m)



[출처] [R프로그래밍] RStudio 선형 회귀 (1) 단순 선형 회귀|작성자 아트


####################################################











par(mar=c(1,1,1,1))


워드클라우드 전에 적어주면 됨

forceNetwork


bounded = TRUE

플롯이 화면 밖으로 안 빠져나가게 함


 linkColour="#afafaf"

선 색 조정


slice(match(position, data))


data안에서 position대로 정렬하고 싶을 때?


ex) 

> position<-c("c", "d", "e", ... , "a")


> match(position, data)

[1] 8  7  6  5  4  3  2  1  9 18 19 20 21 22 23 24 25 10 11 12 13 14 15 16 17

# c는 data의 8번째 행에 위치, a는 data의 17번째 행에 위치


> slice(match(position, data))

# 내가 position에 저장해놓은 순서대로 data의 변수가 정렬됨

ggplot(dat, aes(x=YEAR, y=DAT, color=BRAND)) 

  geom_line(size=1) +

  geom_point(size=1.5) +

  coord_cartesian(ylim=c(0,1069235)) 

%in% 연산자 - 벡터의 값 존재 여부 구하기


ex)

a<-b[b %in% DAY,]

c<-subset(d, DAY %in% c("Mon", "Tue"))

%>% 연산자 - 복수 작업 연결하기


ex)

a <- b %>% group_by(YEAR,BRAND) %>% summarize(TOTAL=sum(TOTAL))

+ Recent posts