Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
#原始的方式 | |
lines = [line.split(',') for line in open('iris.csv')] | |
df = [[float(x) for x in line[:4]] for line in lines[1:]] | |
#使用numpy包 | |
import numpy as np | |
lines = np.loadtxt('iris.csv',delimiter=',',dtype='str') | |
df = lines[1:,:4].astype('float') |
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 | |
x = np.random.randn(100) | |
y = 2*x + np.random.randn(100) | |
%load_ext rpy2.ipython | |
%%R -i x,y -w 500 -h 300 | |
df <- data.frame(x,y) | |
m <- lm(y~x) | |
inter <- m$coef[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
# 假设七个岛人口数分布如下 | |
# 1:7/sum(1:7) | |
# 尝试10万次旅行 | |
trajLength = 1e5 | |
trajectory = rep( 0 , trajLength ) | |
trajectory[1] = 3 # 第一次在第3个岛上 | |
target = function(currentPosition,proposedJump){ | |
tartetposition = currentPosition+proposedJump | |
p = min(1,tartetposition/currentPosition) |
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
myData=c(1,1,1,1,1,1,1,1,1,1,1,0,0,0) | |
# 尝试1万个不同的参数 | |
tryn = 1e4 | |
Theta = sort(runif(tryn)) | |
pTheta = 1/tryn | |
z = sum( myData==1 ) | |
N = length( myData ) | |
# 似然函数 | |
pDataGivenTheta = Theta^z * (1-Theta)^(N-z) | |
pData = sum( pDataGivenTheta * pTheta ) |
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: |
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
# 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
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
# 加载库 | |
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 |