转载 迭代重复加权最小二乘法(IRLS)_熙箔 加权最小二乘法matlab

原文地址:迭代重复加权最小二乘法(IRLS)作者:hhshnsq

1. Theory

http://en.wikipedia.org/wiki/Iteratively_reweighted_least_squares(迭代重复加权最小二乘法)

http://support.sas.com/documentation/cdl/en/statug/63347/HTML/default/viewer.htm#statug_logistic_sect033.htm(SAS Online Help)

2. Implementation in R

The following R cod e uses IRLS algorithm to estimate parametersof Logistic Regression model, and the sample data we used is fromMachine Learning Ex4 - Logistic Regression. Itis a dataset representing 40 students who were admitted to collegeand 40 students who were not admitted, and their correspondinggrades for 2 exams. Our mission, is to build a binaryclassification model that estimates college admission chances basedon a student's scores on two exams(test1 and test2).

To run the codes in R, please follow these steps:

1. Download the csv file by clickling the URL:

http://spreadsheets.google.com/pub?key=0AnypY27pPCJydC1vRVEzM1VJQnNneFo5dWNzR1F5Umc&output=csv

2. Run the R scripts to build the model

# reading data
mydata =read.csv("c://stat/ex4.csv", header = TRUE)


# plots
plot(mydata$test1[mydata$admitted ==0], mydata$test2[mydata$admitted == 0], xlab="test1", ylab="test2",, col="red")
points(mydata$test1[mydata$admitted == 1],mydata$test2[mydata$admitted == 1], col="blue", pch=2)
legend("bottomright", c("not admitted", "admitted"), pch=c(1, 2),col=c("red", "blue") )


[转载]迭代重复加权最小二乘法(IRLS)_熙箔 加权最小二乘法matlab

# build the model
g <- function(x) { return(log(x/(1 - x)))}

g.inv <- function(y) { return(exp(y)/(1 +exp(y))) }

g.prime = function(x) {
return(1/(x * (1 - x)))
}


V = function(mu) {
return(mu * (1 - mu))
}


niter = 10

mu = rep(mean(mydata$admitted),length(mydata$admitted))


for (i in 1:niter) {

Z = g(mu) + g.prime(mu) * (mydata$admitted -mu)

W = 1/(g.prime(mu)^2 * V(mu))

Z.lm = lm(Z ~ test1 + test2, weights = W, data =mydata)

eta = predict(Z.lm)

mu = g.inv(eta)

beta = Z.lm$coef

}

# print estimatedparameters

print(beta)


(Intercept) test1 test2
-16.3787434 0.14834080.1589085

# get 2 points (that will define a line)
x1 = c(min(mydata$test1), max(mydata$test1))

# when ax0 + bx2 + cx3 = 0 is the middle(decisionboundary line),
# so given x1 from sample data, solving to x2, we get:
x2 = (-1/beta[3]) * ((beta[2] * x1) + beta[1])


# plot
plot(x1,x2, type='l', xlab="test1", ylab="test2")
points(mydata$test1[mydata$admitted == 0],mydata$test2[mydata$admitted == 0], col="red")
points(mydata$test1[mydata$admitted == 1],mydata$test2[mydata$admitted == 1], col="blue", pch=2)
legend("bottomright", c("not admitted", "admitted"), pch=c(1, 2),col=c("red", "blue") )

The original R code is from

http://www.stanford.edu/class/stats191/logistic.html,and I changed it a little.


3. Implementation inSAS

proc import datafile="c:statex4.csv"
out=ex4
dbms=csv
replace;
getnames=yes;
run;

proc logistic data=ex4 des;
model admitted=test1test2/tech=fisher;
run;quit;

Analysis of Maximum LikelihoodEstimates
ParameterDFEstimateStandard
Error
Wald
Chi-Square
Pr > ChiSq
Intercept1-16.37873.655920.0717<.0001
test110.14830.040813.20090.0003
test210.15890.041614.56130.0001
Association of PredictedProbabilities and
Observed Responses
Percent Concordant89.5Somers' D0.791
Percent Discordant10.4Gamma0.792
Percent Tied0.1Tau-a0.401
Pairs1600c0.896

  

爱华网本文地址 » http://www.aihuau.com/a/25101014/232563.html

更多阅读

QQ怎么设置二代密保? qq二代密保是什么

QQ怎么设置二代密保?——简介我们刚刚申请成功了一个QQ号,如图:QQ怎么设置二代密保?——方法/步骤QQ怎么设置二代密保? 1、首先,我们登录刚刚申请的这个QQ,如图:QQ怎么设置二代密保? 2、然后我们选择申请密码保护,如图:QQ怎么设置二代密保? 3

GBDTMART 迭代决策树入门教程|简介 mart gbdt

在网上看到一篇对从代码层面理解gbdt比较好的文章,转载记录一下: GBDT(GradientBoostingDecisionTree)又叫MART(MultipleAdditiveRegressionTree),是一种迭代的决策树算法,该算法由多棵决策树组成,所有树的结论累加起来做最终答案。它

(20)转载 武大郎是美女最理想的老公 转载 屠宰美女

转载]武大郎是美女最理想的老公  (2015-03-10 08:10:59)转载▼标签:转载原文地址:武大郎是美女最理想的老公作者:博爱慎行之博客武大郎是美女最理想的老公和潘金莲小姐相比,武大郎先生在中国的知名度就更不用说了,他以自己的矮小丑陋却

转载 Skinship是亲子关系最重要的一环 亲子关系的重要性

说的一点也没错原文地址:Skinship是亲子关系最重要的一环作者:坨坨mamaSkinship这个单词我一直以为是英语,但其实这个单词并不是真正意义上的英语,而是日语里的英语单词,最早指的是小孩刚生下来以后,就马上把他放到妈妈的胸前,让母子进行

声明:《转载 迭代重复加权最小二乘法(IRLS)_熙箔 加权最小二乘法matlab》为网友请持续率性分享!如侵犯到您的合法权益请联系我们删除