All notes
R

# Commandlines

 

###############
# Run scripts.

Rscript a.R

R CMD BATCH a.R
# Check the output
cat a.Rout

R < mean.R
 

 

#!/usr/bin/env Rscript
sayHello <- function(){
print('hello')
}

sayHello()
 

# Commands

 

# Run the script.
source('a.R')

# Install package
install.packages("shiny")
install.packages("rmarkdown")

 

## qnorm, pnorm

 

# Calculate the z value for double-tailed alpha = 0.5.
qnorm(0.025, lower.tail=FALSE)
# 1.96

# find that 85th percentile of a normal distribution whose mean is 70 and whose standard deviation is 3.
qnorm(0.85,mean=70,sd=3)
# [1] 73.1093
# If you were to pick a random member XX from such a population:
# P(X<73.1093)=0.85

pval = 2 * pnorm(1.96, lower.tail=F) # Upper tail.
# 0.5

 

# Shiny

## Installation

 

sudo yum install R

# Before Shiny Server can be installed, the Shiny package must be installed in the system library.

sudo yum install --nogpgcheck shiny-server-1.4.0.rpm

 

/etc/shiny-server/shiny-server.conf

# Define the user we should use when spawning R Shiny processes
run_as shiny;

# Define a top-level server which will listen on a port
server {
# Instruct this server to listen on port 3838
listen 3838;

# Define the location available at the base URL
location / {
#### PRO ONLY ####
# Only up tp 20 connections per Shiny process and at most 3 Shiny processes
# per application. Proactively spawn a new process when our processes reach
# 90% capacity.
utilization_scheduler 20 .9 3;
#### END PRO ONLY ####

# Run this location in 'site_dir' mode, which hosts the entire directory
# tree at '/srv/shiny-server'
site_dir /srv/shiny-server;

# Define where we should put the log files for this location
log_dir /var/log/shiny-server;

# Should we list the contents of a (non-Shiny-App) directory when the user
# visits the corresponding URL?
directory_index on;
}
}

# Setup a flat-file authentication system. {.pro}
auth_passwd_file /etc/shiny-server/passwd;

# Define a default admin interface to be run on port 4151. {.pro}
# Only permit the user named admin to access the admin interface.
}


# Elementary statistics

## Hypothesis testing

### Two-tailed test of population proportion

Null hypothesis: $p=p_0$. $p$ is the true population proportion, and $p_0$ is a hypothesized value.

The test statistic $z$: $$z = \frac{\bar{p}-p_0}{\sqrt{p_0(1-p_0)/n}}$$

The null hypothesis is to be rejected if $|z|\le z_{\alpha/2}$, where $z_{\alpha∕2}$ is the $100(1 - \alpha)$ percentile of the standard normal distribution.

#### Example

Problem: Suppose a coin toss turns up 12 heads out of 20 trials. At .05 significance level, can one reject the null hypothesis that the coin toss is fair?

The null hypothesis is that $p = 0.5$.

 

pbar = 12/20           # sample proportion
p0 = .5                # hypothesized value
n = 20                 # sample size
z = (pbar-p0)/sqrt(p0*(1-p0)/n)
z                      # test statistic
# [1] 0.89443

## Method 1: critical values.
alpha = .05
z.half.alpha = qnorm(1-alpha/2)
c(-z.half.alpha, z.half.alpha)
# [1] -1.9600  1.9600
# z lies between the critical values. Hence, at .05 significance level, we do not reject the null hypothesis that the coin toss is fair.

## Method 2: p value.
pval = 2 * pnorm(z, lower.tail=FALSE)  # upper tail
pval # two-tailed p-value
# [1] 0.37109
# Since p-value turns out to be greater than the .05 significance level, we do not reject the null hypothesis that p = 0.5.

## Method 3
prop.test(12, 20, p=0.5, correct=FALSE)
# 1-sample proportions test without continuity correction.
# The Yates continuity correction is disabled for pedagogical reasons.

# data:  12 out of 20, null probability 0.5
# X-squared = 0.8, df = 1, p-value = 0.3711
# alternative hypothesis: true p is not equal to 0.5
# 95 percent confidence interval:
#   0.38658 0.78119
# sample estimates:
#   p
# 0.6

 

# Components

## rmarkdown

 

rmarkdown::render("input.Rmd")

render("input.Rmd", "pdf_document")
// You can also render all formats defined in an input file with:
render("input.Rmd", "all")

 

 

This metadata included at the top of an Rmd file adds a table of contents and chooses a different HTML theme:

---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---

---
title: "Sample Document"
output:
pdf_document:
toc: true
highlight: zenburn
---