This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(treemap) | |
data <- read.csv('d:/sheet1.csv',T) | |
tmPlot(data, | |
index=c("item", "subitem"), | |
vSize="time1206", | |
vColor="time1106", | |
type="comp", | |
title='苹果公司财务报表可视化', | |
palette='RdBu') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(treemap) | |
data <- read.csv('d:/olympic.csv',T) | |
tmPlot(data, | |
index=c("sports", "events"), | |
vSize="gold", | |
vColor="china", | |
type="value", | |
title='中国奥运金牌分布', | |
fontsize.labels=13, | |
lowerbound.cex.labels=0.7, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 数据读入 | |
data <- read.csv('qq.csv',T,stringsAsFactors=F) | |
data <- data[-nrow(data),] # 最后一行有问题,删除 | |
library(stringr) | |
library(plyr) | |
library(lubridate) | |
library(ggplot2) | |
library(reshape2) | |
library(igraph) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example from Survival Analysis- A Self-Learning Text, Third Edition | |
library(survival) | |
addicts <- read.table('ADDICTS.txt',T) | |
names(addicts) <- c('id','clinic','status', 'survt','prison','dose') | |
# 1. 估计生存函数,观察不同组间的区别 | |
# 建立生存对象 | |
Surv(addicts$survt,addicts$status==1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 原始文件读入 | |
txt <- readLines('txtdm.txt') | |
ignore = ",|:|!|'" | |
stopwords = c('and','edition','for','in','little','of','the','to') | |
txt <- tolower(txt) | |
# 文档分词 | |
doc <- strsplit(txt,' ') | |
# 去除常用词和标点 | |
doc <- lapply(doc,function(x)gsub(ignore,'',x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 加载库 | |
import numpy as np | |
import pandas as pd | |
from ggplot import * | |
# 生成二维随机游走数据 | |
def walk(n): | |
NS = randint(0,2,size=n) | |
WE = 1 - NS | |
xstep = np.random.choice([1,-1],size=n,replace=True) * WE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rem2 <- function (){ | |
library(nlme) | |
a0 <- 9.9 | |
a1 <- 2 | |
# 对6个人重复测量多次 | |
ni <- c(12, 13, 14, 15, 16, 13) | |
nyear <- length(ni) | |
set.seed(205) | |
# 构造x值 | |
xx <- matrix(rep(0, length=max(ni) * nyear), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Data generation of Random Intercept and slope model | |
a0 <- 9.9 | |
a1 <- 2 | |
n <- c(12, 13, 14, 15, 16, 13) | |
npeople <- length(n) | |
set.seed(1) | |
si <- rnorm(npeople, mean = 0, sd = 0.5) # random slope | |
x <- matrix(rep(0, length = max(n) * npeople), | |
ncol = npeople) | |
for (i in 1:npeople){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 广义混合效应模型 | |
glme <- function (){ | |
library(lme4) | |
set.seed(820) | |
ni <- c(12, 13, 14, 15, 16, 13, 11, 17, 13, 16) | |
ndset <- length(ni) | |
xx <- matrix(rep(0, length = max(ni) * ndset), | |
ncol = ndset) | |
bbi <- rnorm(ndset, mean = 0, sd = 1) | |
for (ii in 1:ndset){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 基于《统计模拟和R实现》一书例子 | |
import numpy as np | |
from numpy import random as rm | |
import pandas as pd | |
def simulation(T=4200): | |
t = 0;nA =0;nD=0;n=0;A=[];D=[];N=[];S=[] | |
tA = rm.exponential(10,1) # 客来时间 | |
tD = inf # 客走时间 | |
while True: |