Skip to content

Instantly share code, notes, and snippets.

@ito4303
ito4303 / ggplot2_aes_string.R
Last active October 16, 2023 10:46
Use string to specify variables in aes() function of ggplot2
library(ggplot2)
library(palmerpenguins)
point_plot <- function(d, x, y) {
ggplot(d) +
geom_point(aes(x = .data[[x]], y = .data[[y]]))
}
(var_names <- colnames(penguins))
@ito4303
ito4303 / D-H.R
Last active July 22, 2023 23:24
Estimation of diameter-height curves
# 『たのしいベイズモデリング2』第5章のRコード
# CmdStanR版
# R 4.3.1 Mac, Stan 2.23.2, CmdStanR 0.5.3 で動作確認
library(readxl)
library(dplyr)
library(cmdstanr)
options(mc.cores = parallel::detectCores())
# データファイルのURL
@ito4303
ito4303 / tmap_draw_sapporo.R
Created July 16, 2023 02:43
draw Sapporo city map using the tmap package
library(sf)
library(tmap)
# 参考にしたところ
# https://qiita.com/ocean_f/items/700aff67f3e35266b0fe
# 国土数値情報からデータをダウンロード
# https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N03-v3_1.html
datafile <- file.path("data", "N03-20230101_01_GML",
"N03-23_01_230101.geojson")
@ito4303
ito4303 / zib_regression.Rmd
Last active February 16, 2023 08:31
Zero-inflated beta regression
---
title: "Zero-inflated beta regression"
output: html_notebook
---
## Setup
```{r setup}
library(ggplot2)
library(zoib)
@ito4303
ito4303 / beta_regression.Rmd
Created February 15, 2023 10:32
Beta regression using R and Stan
---
title: "Beta regression"
output: html_notebook
---
## Setup
```{r setup}
library(ggplot2)
library(betareg)
@ito4303
ito4303 / lrtest.Rmd
Created December 22, 2022 06:50
Likelihood ratio test in R (using base functions or lrtest function)
---
title: "R Notebook"
output: html_notebook
---
## An example of likelihood ratio tests in R
```{r setup}
library(lmtest)
library(palmerpenguins)
@ito4303
ito4303 / glht.Rmd
Created December 22, 2022 05:19
An example to use glht function in the multcomp package
---
title: "R Notebook"
output: html_notebook
---
## An example to use glht function in the multcomp package
```{r setup}
library(multcomp)
library(palmerpenguins)
@ito4303
ito4303 / Lotka-Volterra_competition.Rmd
Last active March 28, 2023 08:21
R Notebook for Lotka-Volterra competition model
---
title: "R Notebook"
output: html_notebook
---
```{r setup}
library(ggplot2)
library(cmdstanr)
## Lotka-Volterra competitive equation
@ito4303
ito4303 / Lotka-Volterra_competition.stan
Created December 15, 2022 22:58
Stan code to fit Lotka-Volterra competition model
functions {
vector dz_dt(real t, vector x, array[] real theta) {
array[2] real r = theta[1:2];
array[2] real K = theta[3:4];
array[2] real alpha = theta[5:6];
real dx1_dt = r[1] * x[1] * (1 - (x[1] + alpha[1] * x[2]) / K[1]);
real dx2_dt = r[2] * x[2] * (1 - (x[2] + alpha[2] * x[1]) / K[2]);
return [ dx1_dt, dx2_dt ]';
}
}
@ito4303
ito4303 / post-model-selection_2.R
Created April 10, 2022 00:26
Issues related to post-model-selection estimators
library(MASS)
library(AICcmodavg)
library(parallel)
# Set number of CPU cores
options(cl.cores = 8)
R <- 100000
N <- 100