Displaying 20 results from an estimated 10000 matches similar to: "Solved: linear regression example using MLE using optim()"
2005 May 30
1
Trying to write a linear regression using MLE and optim()
I wrote this:
# Setup problem
x <- runif(100)
y <- 2 + 3*x + rnorm(100)
X <- cbind(1, x)
# True OLS --
lm(y ~ x)
# OLS likelihood function --
ols.lf <- function(theta, K, y, X) {
beta <- theta[1:K]
sigma <- exp(theta[K+1])
e <- (y - X%*%beta)/sigma
logl <- sum(log(dnorm(e)))
return(logl)
}
optim(c(2,3,0), ols.lf, gr=NULL,
method="BFGS",
2005 Jun 06
1
A performance anomaly
I wrote a simple log likelihood (for the ordinary least squares (OLS)
model), in two ways. The first works out the likelihood. The second
merely calls the first, but after transforming the variance parameter,
so as to allow an unconstrained maximisation. So the second suffers a
slight cost for one exp() and then it pays the cost of calling the first.
I did performance measurement. One would
2011 Dec 01
1
Estimation of AR(1) Model with Markov Switching
Dear R users,
I have been trying to obtain the MLE of the following model
state 0: y_t = 2 + 0.5 * y_{t-1} + e_t
state 1: y_t = 0.5 + 0.9 * y_{t-1} + e_t
where e_t ~ iidN(0,1)
transition probability between states is 0.2
I've generated some fake data and tried to estimate the parameters using the
constrOptim() function but I can't get sensible answers using it. I've tried
using
2011 Mar 28
1
maximum likelihood accuracy - comparison with Stata
Hi everyone,
I am looking to do some manual maximum likelihood estimation in R. I
have done a lot of work in Stata and so I have been using output
comparisons to get a handle on what is happening.
I estimated a simple linear model in R with lm() and also my own
maximum likelihood program. I then compared the output with Stata.
Two things jumped out at me.
Firstly, in Stata my coefficient
2008 Aug 12
2
Maximum likelihood estimation
Hello,
I am struggling for some time now to estimate AR(1) process for commodity price time series. I did it in STATA but cannot get a result in R.
The equation I want to estimate is: p(t)=a+b*p(t-1)+error
Using STATA I get 0.92 for a, and 0.73 for b.
Code that I use in R is:
p<-matrix(data$p) # price at time t
lp<-cbind(1,data$lp) # price at time t-1
2009 Jul 01
2
Difficulty in calculating MLE through NLM
Hi R-friends,
Attached is the SAS XPORT file that I have imported into R using following code
library(foreign)
mydata<-read.xport("C:\\ctf.xpt")
print(mydata)
I am trying to maximize logL in order to find Maximum Likelihood Estimate (MLE) of 5 parameters (alpha1, beta1, alpha2, beta2, p) using NLM function in R as follows.
# Defining Log likelihood - In the function it is noted as
2008 Apr 05
2
How to improve the "OPTIM" results
Dear R users,
I used to "OPTIM" to minimize the obj. function below. Even though I used
the true parameter values as initial values, the results are not very good.
How could I improve my results? Any suggestion will be greatly appreciated.
Regards,
Kathryn Lord
#------------------------------------------------------------------------------------------
x = c(0.35938587,
2008 Apr 05
2
How to improve the "OPTIM" results
Dear R users,
I used to "OPTIM" to minimize the obj. function below. Even though I used
the true parameter values as initial values, the results are not very good.
How could I improve my results? Any suggestion will be greatly appreciated.
Regards,
Kathryn Lord
#------------------------------------------------------------------------------------------
x = c(0.35938587,
2010 Dec 16
1
Optim function with meta parameters
Hi guys.
I have a dataset with 4 columns. In the first and second column I have the
same qualitative variable referred to different teams of people. There are
10 teams in total and they compete against each other to perform a certain
task whose result is stored in the third column for the team recorded in the
first column, and in the fourth column for the team in the second column.
For example,
2009 Apr 26
1
Stochastic Gradient Ascent for logistic regression
Hi. guys,
I am trying to write my own Stochastic Gradient Ascent for logistic
regression in R. But it seems that I am having convergence problem.
Am I doing anything wrong, or just the data is off?
Here is my code in R -
lbw <-
read.table("http://www.biostat.jhsph.edu/~ririzarr/Teaching/754/lbw.dat"
, header=TRUE)
attach(lbw)
lbw[1:2,]
low age lwt race smoke ptl ht ui ftv
2009 Oct 31
1
Help me improving my code
Hi,
I am new to R. My problem is with the ordered logistic model. Here is my
question:
Generate an order discrete variable using the variable
wrwage1 = wages in first full calendar quarter after benefit application
in the following way:
*
wage*1*Ordered *=
1 *if*0 *· wrwage*1 *< *1000
2 *if*1000 *· wrwage*1 *< *2000
3 *if*2000 *· wrwage*1 *< *3000
4 *if*3000 *· wrwage*1 *<
2008 Sep 19
2
Error: function cannot be evaluated at initial parameters
I have an error for a simple optimization problem. Is there anyone knowing
about this error?
lambda1=-9
lambda2=-6
L<-function(a){
s2i2f<-(exp(-lambda1*(250^a)-lambda2*(275^a-250^a))
-exp(-lambda1*(250^a)-lambda2*(300^a-250^a)))
logl<-log(s2i2f)
return(-logl)}
optim(1,L)
Error in optim(1, L) : function cannot be evaluated at initial parameters
Thank you in advance
--
View this
2008 Mar 19
1
problem with optim and integrate
Dear all,
I want to min "integrate( (p1*dnorm+p2*dnorm+p3*dnorm)^(1.3))" for p, mu,
and sigma.
So, I have to estimate 8 parameters(p3=1-p1-p2).
I got this warning-"Error in integrate(numint, lower = -Inf, upper = Inf) :
non-finite function value."
My questions are
How could I fix it? I tried to divide into several intervals and sum up, but
I got same message.
My code is
2006 Apr 05
2
R2WinBUGS error
Dear R-help,
I'm using the R2WinBUGS package and getting an error message:
Error in file(file, "r") : unable to open connection
In addition: Warning message:
cannot open file 'codaIndex.txt', reason 'No such file or
directory'
I'm using R 2.2.1 and WinBUGS 1.4.1 on a windows machine (XP). My R code
and WinBUGS code is given below.
2011 May 23
6
Reading Data from mle into excel?
Hi there,
I ran the following code:
vols=read.csv(file="C:/Documents and Settings/Hugh/My Documents/PhD/Swaption
vols.csv"
, header=TRUE, sep=",")
X<-ts(vols[,2])
#X
dcOU<-function(x,t,x0,theta,log=FALSE){
Ex<-theta[1]/theta[2]+(x0-theta[1]/theta[2])*exp(-theta[2]*t)
Vx<-theta[3]^2*(1-exp(-2*theta[2]*t))/(2*theta[2])
dnorm(x,mean=Ex,sd=sqrt(Vx),log=log)
}
2005 Jun 29
2
MLE with optim
Hello,
I tried to fit a lognormal distribution by using optim. But sadly the output
seems to be incorrect.
Who can tell me where the "bug" is?
test = rlnorm(100,5,3)
logL = function(parm, x,...) -sum(log(dlnorm(x,parm,...)))
start = list(meanlog=5, sdlog=3)
optim(start,logL,x=test)$par
Carsten.
[[alternative HTML version deleted]]
2007 Oct 09
3
How do I obtain the design matrix of an lm()?
I am using the clever formula notation of R to first do an OLS. E.g. I
say
m <- lm(y ~ x + f)
where f is a factor, and R automatically constructs the dummy
variables. Very nice.
I need to then go on to do some other ML estimation using the same
design matrix that's used for the OLS. I could, of course, do this
manually. But it seems that lm() has done all this hard work. I wonder
if
2005 Jun 07
1
R and MLE
I learned R & MLE in the last few days. It is great! I wrote up my
explorations as
http://www.mayin.org/ajayshah/KB/R/mle/mle.html
I will be most happy if R gurus will look at this and comment on how
it can be improved.
I have a few specific questions:
* Should one use optim() or should one use stats4::mle()?
I felt that mle() wasn't adding much value compared with optim, and
2008 Dec 04
0
integration within maximum likelihood
Hi:
I'm trying to estimate a latent variable model in mnl discrete choice
framework using R. I need to do first a uni dimensional integral
within each observation (row) in the database and then sum over
observations. I'm stacked in the point shown below. Apparently I have
a dimensionality problem in the definition of the integral. Maybe it
does not identify that what I need is only one
2005 Sep 25
1
Question on lm(): When does R-squared come out as NA?
I have a situation with a large dataset (3000+ observations), where
I'm doing lags as regressors, where I get:
Call:
lm(formula = rj ~ rM + rM.1 + rM.2 + rM.3 + rM.4)
Residuals:
1990-06-04 1994-11-14 1998-08-21 2002-03-13 2005-09-15
-5.64672 -0.59596 -0.04143 0.55412 8.18229
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.003297 0.017603