当前位置:首页 > R统计建模复习提纲 - 图文
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))
共分享92篇相关文档