Skip to content

Commit

Permalink
《现代应用统计》
Browse files Browse the repository at this point in the history
  • Loading branch information
XiangyunHuang committed Feb 29, 2024
1 parent de1939c commit 1789fba
Show file tree
Hide file tree
Showing 60 changed files with 13,606 additions and 15 deletions.
65 changes: 61 additions & 4 deletions .github/workflows/quarto-book-netlify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ jobs:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
CMDSTAN_VERSION: "2.34.1"
PANDOC_VERSION: "3.1.11"
RETICULATE_PYTHON_ENV: /opt/.virtualenvs/r-tensorflow
steps:
- uses: actions/checkout@v4

- name: Install Quarto
uses: quarto-dev/quarto-actions/setup@v2
with:
version: 1.4.549
version: 1.4.550

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true
r-version: '4.3.2'
extra-repositories: 'https://mc-stan.org/r-packages https://inla.r-inla-download.org/R/stable'

- uses: r-lib/actions/setup-r-dependencies@v2

Expand All @@ -46,10 +50,63 @@ jobs:
# install full prebuilt version
TINYTEX_INSTALLER: TinyTeX

- name: Render book to all format
# Add any command line argument needed
- name: Setup Python
run: |
quarto render
pip3 install virtualenv
mkdir -p /opt/.virtualenvs/r-tensorflow
virtualenv -p /usr/bin/python3 $RETICULATE_PYTHON_ENV
source $RETICULATE_PYTHON_ENV/bin/activate
pip3 install -r requirements.txt
python -m spacy download en_core_web_sm
python -m spacy download zh_core_web_sm
deactivate
- name: Setup CmdStan
run: |
curl -fLo cmdstan-${CMDSTAN_VERSION}.tar.gz https://github.com/stan-dev/cmdstan/releases/download/v${CMDSTAN_VERSION}/cmdstan-${CMDSTAN_VERSION}.tar.gz
mkdir -p /opt/cmdstan/
tar -xzf cmdstan-${CMDSTAN_VERSION}.tar.gz -C /opt/cmdstan/
make build -C /opt/cmdstan/cmdstan-${CMDSTAN_VERSION}
rm cmdstan-${CMDSTAN_VERSION}.tar.gz
- name: Install Fonts From System
run: |
sudo apt-get install -y ghostscript graphviz optipng
sudo apt-get install -y fonts-noto-core fonts-noto-cjk
fc-list | grep 'noto' | sort
fc-list | sort
- name: Install LaTeX packages
run: |
if(!require('tinytex')) install.packages('tinytex')
tinytex::tlmgr_install(readLines("texlive.txt"))
tinytex::tl_pkgs()
tikzDevice::tikzTest("m", engine = "xetex")
tikzDevice::tikzTest("$\\sin(x^2/2 - y^2/4 + 3) \\cos(2 x + 1 - \\exp(y))$")
shell: Rscript {0}

- name: Reinstall R packages from source
run: |
install.packages(c("Matrix", "MatrixModels", "rjags", "lme4", "TMB", "glmmTMB"), repos = "https://cran.r-project.org/", type = "source")
shell: Rscript {0}

- name: Setup magick
run: |
sudo sed -i_bak \
's/name="disk" value="1GiB"/name="disk" value="8GiB"/' \
/etc/ImageMagick-6/policy.xml
- name: Render Book
run: |
source $RETICULATE_PYTHON_ENV/bin/activate
quarto check
quarto render --to html
shell: bash
env:
RETICULATE_PYTHON_ENV: /opt/.virtualenvs/r-tensorflow
RETICULATE_PYTHON: /opt/.virtualenvs/r-tensorflow/bin/python
CMDSTAN: /opt/cmdstan/cmdstan-${{ env.CMDSTAN_VERSION }}


- name: Deploy book to bookdown.org
if: github.event_name == 'push'
Expand Down
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,24 @@ masr.tex
_book/
old/
rsconnect/

*.log
*-tikzDictionary
*.aux

code/poisson_log_glm
code/bernoulli_logit_glm_normal
code/bernoulli_logit_glm_lasso
code/bernoulli_logit_glm_horseshoe
code/gaussian_process_simu
code/gaussian_process_fitted
code/gaussian_process_pred
code/multi_normal_simu
code/multi_normal_fitted
code/rongelap_poisson_pred
code/rongelap_poisson_processes
code/faithful_finite_mixtures
code/faithful_2d_finite_mixtures
code/eight_schools
code/rats
code/stochastic_volatility_models
52 changes: 50 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,59 @@ Authors@R: c(
)
URL: https://github.com/XiangyunHuang/masr
Depends:
R (>= 4.2.0)
R (>= 4.3.0)
Imports:
abess,
bayesplot,
BH,
blme,
brms,
cmdstanr (>= 0.7.1),
expm,
fastmatrix,
fGarch (>= 4031.90),
fmesher,
FRK (>= 2.2.0),
glmmTMB,
glmnet,
GLMMadaptive,
geodata (>= 0.4.13),
ggplot2 (>= 3.5.0),
hglm,
INLA (>= 23.9.9),
keras,
knitr,
quarto
lars,
lavaan,
lme4,
loo (>= 2.6.0),
MCMCglmm,
ncvreg,
pls,
projpred,
randomForest,
reticulate,
rjags,
rstan (>= 2.32.3),
rpart.plot,
sf (>= 1.0.9),
spaMM,
spdep,
splancs,
spatstat,
stars (>= 0.6.0),
tensorflow,
tidycensus,
tikzDevice,
xgboost,
VGAM
Suggests:
quarto,
rsconnect
LinkingTo: StanHeaders (>= 2.32.2), RcppParallel (>= 5.1.0)
SystemRequirements: pgf (>= 3.00), JAGS 4.x.y
Additional_repositories:
https://mc-stan.org/r-packages/,
https://inla.r-inla-download.org/R/stable
Encoding: UTF-8
License: CC NC ND 4.0
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
html:
quarto render
quarto render --to html
100 changes: 100 additions & 0 deletions _common.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
knitr::knit_hooks$set(par = function(before, options, envir) {
if (before && options$fig.show != "none") {
par(
mar = c(4, 4, .5, .5)
)
}
})

knitr::opts_chunk$set(
comment = "#>"
)

if (grepl(x = sessionInfo()$running, pattern = "macOS")) {
# 准备 Noto 中英文字体
sysfonts::font_paths(new = "~/Library/Fonts/")
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJKsc-Regular.otf",
bold = "NotoSerifCJKsc-Bold.otf"
)
## 黑体
sysfonts::font_add(
family = "Noto Sans CJK SC",
regular = "NotoSansCJKsc-Regular.otf",
bold = "NotoSansCJKsc-Bold.otf"
)
} else if (grepl(x = sessionInfo()$running, pattern = "Fedora")) { # Github Action custom Docker Container Based on Fedora
sysfonts::font_paths(new = c(
"/usr/share/fonts/google-noto-serif-cjk-fonts" # Fedora 39
))
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJK-Regular.ttc",
bold = "NotoSerifCJK-Bold.ttc"
)
} else if (grepl(x = sessionInfo()$running, pattern = "Rocky")) {
sysfonts::font_paths(new = c(
"/usr/share/fonts/google-noto-cjk/" # Rocky Linux 9
))
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJK-Regular.ttc",
bold = "NotoSerifCJK-Bold.ttc"
)
## 黑体
sysfonts::font_add(
family = "Noto Sans CJK SC",
regular = "NotoSansCJK-Regular.ttc",
bold = "NotoSansCJK-Bold.ttc"
)
} else { # Ubuntu
sysfonts::font_paths(new = c(
"/usr/share/fonts/opentype/noto/",
"/usr/share/fonts/truetype/noto/"
))
## 宋体
sysfonts::font_add(
family = "Noto Serif CJK SC",
regular = "NotoSerifCJK-Regular.ttc",
bold = "NotoSerifCJK-Bold.ttc"
)
## 黑体
sysfonts::font_add(
family = "Noto Sans CJK SC",
regular = "NotoSansCJK-Regular.ttc",
bold = "NotoSansCJK-Bold.ttc"
)
}

# 设置 Web GL 渲染
options(rgl.useNULL = TRUE)
options(rgl.printRglwidget = TRUE)

# tinytex 不要自动安装宏包
options(tinytex.install_packages = FALSE)
options(
tinytex.engine = "xelatex",
tikzDefaultEngine = "xetex",
tikzDocumentDeclaration = "\\documentclass[tikz]{standalone}\n",
tikzXelatexPackages = c(
"\\usepackage[fontset=fandol]{ctex}",
"\\usepackage{amsfonts,mathrsfs,amssymb}\n"
)
)
# 用 magick 将 pdf 格式图片转化为 png 格式
to_png <- function(fig_path) {
png_path <- sub("\\.pdf$", ".png", fig_path)
magick::image_write(magick::image_read_pdf(fig_path),
format = "png", path = png_path,
density = 300, quality = 100
)
return(png_path)
}

# 设置 Python
Sys.setenv(RETICULATE_PYTHON = "/opt/.virtualenvs/r-tensorflow/bin/python")
Sys.setenv(RETICULATE_PYTHON_ENV = "/opt/.virtualenvs/r-tensorflow")
48 changes: 44 additions & 4 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,42 @@ book:
chapters:
- index.qmd
- intro.qmd
- part: "机器学习"
chapters:
- classification-problems.qmd
- clustering-problems.qmd
- regression-problems.qmd
- part: "贝叶斯建模"
chapters:
- probabilistic-reasoning-framework.qmd
- generalized-linear-models.qmd
- hierarchical-normal-models.qmd
- mixed-effects-models.qmd
- generalized-additive-models.qmd
- gaussian-processes-regression.qmd
- time-series-regression.qmd
- part: "空间分析"
chapters:
- analyze-point-pattern.qmd
- analyze-spatial-data.qmd
- analyze-areal-data.qmd
- references.qmd
appendices:
- notations.qmd
- matrix-operations.qmd
repo-branch: main
search: true
downloads: [pdf]

bibliography: references.bib
bibliography:
- references.bib
- packages.bib

format:
html:
theme: cosmo
# code-fold: true
# code-tools: true
code-fold: true
code-tools: true
crossref:
chapters: true
docx:
Expand All @@ -33,8 +57,24 @@ format:
pdf:
documentclass: book
include-in-header: preamble.tex
classoption: [UTF8,twoside,openany,table]
include-before-body:
text: |
\ifdefined\appendixname
\renewcommand*\appendixname{附录}
\else
\newcommand\appendixname{附录}
\fi
include-after-body:
text: |
\printindex
classoption:
- UTF8
- twoside
- openany
- table
keep-tex: true
latex-auto-install: false
mathspec: true
lof: true
lot: true
geometry:
Expand Down
Loading

0 comments on commit 1789fba

Please sign in to comment.