Giter VIP home page Giter VIP logo

bayesiansocialscience's Introduction

`사회과학자를 위한 데이터 과학’ 코드 저장소 이용방법

박종희 2020-09-25

소개

이 문서는 사회과학자를 위한 데이터 과학(박종희 2020)에 소개된 R코드를 다운 받아 사용하는 방법을 소개합니다.

사용방법

먼저 현재 사용자의 R 워킹 디렉토리를 확인합니다. 이 곳에 집파일을 다운 받아 풀 것입니다.

## 현재 워킹 디렉토리가 어디인지 확인
getwd()
## 필요하면 원하는 위치로 변경: "."을 "where/you/want"로 바꾸면 됨.
## setwd(dir = ".")

전체코드를 집파일로 다운로드 받습니다. 깃허브의 “Download ZIP” 버튼을 이용해서 수동으로 진행해도 됩니다.

download.file(url = "https://github.com/jongheepark/BayesianSocialScience/archive/master.zip", 
              destfile = "BayesianSocialScience-master.zip")

다운받은 파일을 풉(unzip)니다.

unzip(zipfile = "BayesianSocialScience-master.zip")

폴더 안의 파일들을 확인합니다.

list.files("BayesianSocialScience-master")
##  [1] "01-intro.R"                                       
##  [2] "02-probability.R"                                 
##  [3] "03-distribution.R"                                
##  [4] "04-association.R"                                 
##  [5] "05-regression.R"                                  
##  [6] "06-ols.R"                                         
##  [7] "07-mle.R"                                         
##  [8] "08-bayes.R"                                       
##  [9] "09-bayesinference.R"                              
## [10] "10-history.R"                                     
## [11] "11-extension.R"                                   
## [12] "12-network.R"                                     
## [13] "index.R"                                          
## [14] "long-term-cereal-yields-in-the-united-kingdom.csv"
## [15] "README.md"

해당 폴더로 디렉토리 위치를 변경한 뒤, Ch.5에 나온 회귀분석 예제를 실행해 봅니다.

setwd("BayesianSocialScience-master")
source("05-regression.R", echo=TRUE)
## 
## R > source("index.R")
## 
## R > library(UsingR)
## 
## R > data(galton)
## 
## R > ggplot(galton, aes(x = parent, y = child)) + geom_point(size = 4, 
## +     alpha = 0.1, col = "brown") + xlab("부모의 중간 신장") + 
## +     ylab("자녀의 신장") + .... [TRUNCATED]

## 
## R > center <- function(x) {
## +     out <- (x - mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE)
## +     return(out)
## + }
## 
## R > galton.cen <- data.frame(apply(galton, 2, center))
## 
## R > rho.test <- cor.test(galton.cen[, 1], galton.cen[, 
## +     2])
## 
## R > rho.test
## 
##  Pearson's product-moment correlation
## 
## data:  galton.cen[, 1] and galton.cen[, 2]
## t = 15.711, df = 926, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.4064067 0.5081153
## sample estimates:
##       cor 
## 0.4587624 
## 
## 
## R > rho <- rho.test$estimate
## 
## R > galton.lm <- lm(child ~ parent, data = galton.cen)
## 
## R > stargazer(galton.lm, header = FALSE, type = "latex", 
## +     title = "골튼의 신장유전 자료에 대한 회귀분석: 자녀의 신장 ~ 부모의 중간 신장", 
## +     label = "galton.reg")
## 
## \begin{table}[!htbp] \centering 
##   \caption{골튼의 신장유전 자료에 대한 회귀분석: 자녀의 신장 ~ 부모의 중간 신장} 
##   \label{galton.reg} 
## \begin{tabular}{@{\extracolsep{5pt}}lc} 
## \\[-1.8ex]\hline 
## \hline \\[-1.8ex] 
##  & \multicolumn{1}{c}{\textit{Dependent variable:}} \\ 
## \cline{2-2} 
## \\[-1.8ex] & child \\ 
## \hline \\[-1.8ex] 
##  parent & 0.459$^{***}$ \\ 
##   & (0.029) \\ 
##   & \\ 
##  Constant & 0.000 \\ 
##   & (0.029) \\ 
##   & \\ 
## \hline \\[-1.8ex] 
## Observations & 928 \\ 
## R$^{2}$ & 0.210 \\ 
## Adjusted R$^{2}$ & 0.210 \\ 
## Residual Std. Error & 0.889 (df = 926) \\ 
## F Statistic & 246.839$^{***}$ (df = 1; 926) \\ 
## \hline 
## \hline \\[-1.8ex] 
## \textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
## \end{tabular} 
## \end{table} 
## 
## R > ggplot(galton.lm, aes(x = parent, y = child)) + geom_smooth(method = "lm", 
## +     aes(fill = "confidence"), show.legend = F, alpha = 0.2, col = "n ..." ... [TRUNCATED]

## 
## R > x <- seq(-3, 3, length = 100)
## 
## R > plot(x, exp(x), col = addTrans("brown", 50), type = "p", 
## +     cex = 0.5, pch = 19)

## 
## R > grid()
## 
## R > abline(v = 0, col = "gray40", lty = 3)
## 
## R > lines(x, x + 1, col = "brown", lwd = 1)
## 
## R > text(x[90], exp(x[90]) - 0.5, "y = exp(x)")
## 
## R > text(x[80], x[80] - 0.5, "y = x + 1")
## 
## R > DiagrammeR::grViz("digraph {\n  graph [layout = dot, rankdir = TB]\n  \n  node [shape = rectangle]        \n  rec1 [label = 'Step 1. Significance' ..." ... [TRUNCATED]

bayesiansocialscience's People

Contributors

jongheepark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bayesiansocialscience's Issues

오류 http://gapm.io/dl_pop

안녕하세요.
교수님책을 구입해서 스터디를 하고 있는 1인입니다.
파일 다운로드가 안되고 갭마인더 홈페이지에서도 다운되는 자료가 책에서 사용하는 것과 다릅니다.
혹시 해당 엑셀파일을 따로 올려주실 수 없을까요?

jhp_report 함수 실행결과가 책과 다릅니다. 무엇이 문제일까요?

책에 명시된 순서대로 쭉 진행하면서 아래의 스크립트를 실행하면 콘솔창에만 저렇게 나타납니다.
이 스크립트 뿐만 아니라 다른 것도 마찬가지입니다.
왜 그럴까요?

(script)
reg1 <- lm(prestige ~ education, data=Prestige)
jhp_report(reg1, title="직업별 명성과 교육수준의 관계",
label="tab:prestige1",
dep.var.labels = "prestige")

(콘솔창)
\begin{table}[!htbp] \centering
\caption{직업별 명성과 교육수준의 관계}
\label{tab:prestige1}
\begin{tabular}{@{\extracolsep{5pt}}lc}
\[-1.8ex]\hline
\hline \[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \
\cline{2-2}
\[-1.8ex] & prestige \
\hline \[-1.8ex]
education & 5.388$^{}$ \
& (0.317) \
& \
Constant & $-$10.841$^{
}$ \
& (3.528) \
& \
\hline \[-1.8ex]
Observations & 98 \
R$^{2}$ & 0.751 \
Adjusted R$^{2}$ & 0.748 \
\hline
\hline \[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{}$p$<$0.1; $^{}$p$<$0.05; $^{}$p$<$0.01} \
\end{tabular}
\end{table}

페이지 230 내용 질의드립니다.

위 척도 정의를 이용하여 모형1과 모형2를 비교하면 MAE와 RMSE, 그리고 R-squared에서 모두 로그소득이 소득보다 더 뛰어난 성능을 보여줌을 확인할 수 있다.

위의 내용을 summary(results)를 보고 수치적으로 어떻게 이해를 할 수 있는지 통계비전공자로서 잘 이해가 되지 않아서 이렇게 여쭈어봅니다. 위 내용에서 조금만 더 부가설명이 가능하실지 요청드려봅니다.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.