楼主: gavinking
13061 10

[问答] plot 似然函数 出错 [推广有奖]

  • 1关注
  • 0粉丝

本科生

98%

还不是VIP/贵宾

-

威望
0
论坛币
34 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
1176 点
帖子
102
精华
0
在线时间
108 小时
注册时间
2008-3-16
最后登录
2018-2-21

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
par.est.theta <- function(sample){
n=length(sample)
log.lik<-function(theta){
  return(-n*log(pi)-sum(log(1+(theta-sample)^2)));
}
diff.f<-function(theta){
return(-2*sum((theta-sample)/(1+(theta-sample)^2)))
}
hess.f<-function(theta){
return(matrix(-2*sum((1-(theta-sample)^2)/(1+(theta-sample)^2)^2),nrow=1));
}
theta.value=seq(-10,10,by=1);
plot(theta.value,log.lik(theta.value),xlab="parameter",
        ylab="log-likelihood", type="l");
theta.est=nlminb(start=-11,-log.lik,-diff.f,-hess.f,lower=-10,upper=10);
return(theta.est);
}
a=c(1.77,-.23,2.76,3.8,3.47,56.75,-1.34,4.24,-2.44,3.29,3.711,-2.4,4.53,-.07,-1.05,13.87,-2.53,-1.75,0.27,43.21)
par.est.theta(a)

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:plot 似然函数 Theta Est ETA

You got a dream, you gotta protect it.
沙发
gavinking 发表于 2012-2-13 05:29:44 |只看作者 |坛友微信交流群
上面太复杂,简化一下问题:

a=c(1.77,-.23,2.76,3.8,3.47,56.75,-1.34,4.24,-2.44,3.29,3.711,-2.4,4.53,-.07,-1.05,13.87,-2.53,-1.75,0.27,43.21)
sample=a
n=length(sample)
log.lik<-function(theta){
  return(-n*log(pi)-sum(log(1+(theta-sample)^2)));
}
theta.value=seq(-10,10,by=1);
plot(theta.value,log.lik(theta.value),xlab="parameter",
        ylab="log-likelihood", type="l");
You got a dream, you gotta protect it.

使用道具

藤椅
gavinking 发表于 2012-2-13 05:31:02 |只看作者 |坛友微信交流群
提示:
Error in xy.coords(x, y, xlabel, ylabel, log) :
  'x' and 'y' lengths differ
In addition: Warning message:
In theta - sample :
  longer object length is not a multiple of shorter object length
You got a dream, you gotta protect it.

使用道具

板凳
gavinking 发表于 2012-2-13 05:33:47 |只看作者 |坛友微信交流群
就是一个简单的plot,设置x为theta.value,y为log.lik函数值。不明白为什么出错
You got a dream, you gotta protect it.

使用道具

报纸
qoiqpwqr 发表于 2012-2-13 06:09:30 |只看作者 |坛友微信交流群
你的log.lik的返回值只有一个数,和theta.value的长度不一样,当然会报错了。
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
aspenroad + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

地板
qoiqpwqr 发表于 2012-2-13 06:11:14 |只看作者 |坛友微信交流群
解决办法:

1. 用循环
2. 在plot前面加上
log.lik <- Vectorize(log.lik)
已有 1 人评分学术水平 收起 理由
aspenroad + 1 热心帮助其他会员

总评分: 学术水平 + 1   查看全部评分

使用道具

7
gavinking 发表于 2012-2-13 06:39:55 |只看作者 |坛友微信交流群
qoiqpwqr 发表于 2012-2-13 06:11
解决办法:

1. 用循环
谢谢谢~
二楼 问题解决了。
可是一楼的plot还是存在问题……
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
You got a dream, you gotta protect it.

使用道具

8
gavinking 发表于 2012-2-13 06:57:09 |只看作者 |坛友微信交流群
qoiqpwqr 发表于 2012-2-13 06:11
解决办法:

1. 用循环
没问题了,谢谢版主~~
You got a dream, you gotta protect it.

使用道具

9
trier2006 发表于 2012-2-13 08:58:40 |只看作者 |坛友微信交流群
友情帮顶
最好的医生是自己,最好的药物是时间……

使用道具

10
zhangyangsmith 发表于 2012-2-14 05:59:34 |只看作者 |坛友微信交流群
Another way of define log.lik, others remain the same:
  1. log.lik <- function(theta) {return(-n*log(pi)-rowSums(1+outer(theta, sample, "-")^2))}
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 07:25