R复习提纲
截取了些东西,有需要的可以下来看看。——何斌
1. 会用R写牛顿算法求极大似然估计及置信区间。
Newtons<-function (fun, x, ep=1e-5, it_max=100){ index<-0; k<-1
while (k<=it_max){
x1 <- x; obj <- fun(x);
x <- x - solve(obj$J, obj$f); norm <- sqrt((x-x1) %*% (x-x1)) if (norm } k<-k+1 } obj <- fun(x); list(root=x, it=k, index=index, FunVal= obj$f) } funs<-function(x){ f<-c(x[1]^2+x[2]^2-5, (x[1]+1)*x[2]-(3*x[1]+1)) J<-matrix(c(2*x[1], 2*x[2], x[2]-3, x[1]+1),nrow=2, byrow=T) list(f=f, J=J) } Newtons(funs, c(0,1)) interval_estimate1<-function(x, sigma=-1, alpha=0.05){ n<-length(x); xb<-mean(x) if (sigma>=0){ tmp<-sigma/sqrt(n)*qnorm(1-alpha/2); df<-n } else{ tmp<-sd(x)/sqrt(n)*qt(1-alpha/2,n-1); df<-n-1 } data.frame(mean=xb, df=df, a=xb-tmp, b=xb+tmp) } 2. 会计算随机变量的函数的密度函数和相关的事件概率,会求相应的模型参数的mle x <- rcauchy(1000,1) f <- function(p) sum((x-p)/(1+(x-p)^2)) out <- uniroot(f, c(0, 5)) loglike <- function(p) sum(log(1+(x-p)^2)) out <- optimize(loglike, c(0, 5)) 3. 会使用正确的统计检验方法来得到合理的结果。包括t.test, Wilcox.test, fisher.test, chisq.test,binom.test, ks.test, mcnemar.test,cor.test,var.test的使用条件。 X<-c(10.1, 10, 9.8, 10.5, 9.7, 10.1, 9.9, 10.2, 10.3, 9.9) interval_estimate1(X) > t.test(X) One Sample t-test data: X t = 131.5854, df = 9, p-value = 4.296e-16 alternative hypothesis: true mean is not equal to 0 95 percent confidence interval: 9.877225 10.222775 sample estimates: mean of x 10.05 x<-rnorm(12, 501.1, 2.4) y<-rnorm(17, 499.7, 4.7) interval_estimate2(x, y, var.equal=TRUE)