similar to: Trying to write a linear regression using MLE and optim()

Displaying 20 results from an estimated 9000 matches similar to: "Trying to write a linear regression using MLE and optim()"

2005 May 31
1
Solved: linear regression example using MLE using optim()
Thanks to Gabor for setting me right. My code is as follows. I found it useful for learning optim(), and you might find it similarly useful. I will be most grateful if you can guide me on how to do this better. Should one be using optim() or stats4::mle? set.seed(101) # For replicability # Setup problem X <- cbind(1, runif(100)) theta.true <- c(2,3,1) y <- X
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
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
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
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
2010 Sep 02
1
Help on glm and optim
Dear all, I'm trying to use the "optim" function to replicate the results from the "glm" using an example from the help page of "glm", but I could not get the "optim" function to work. Would you please point out where I did wrong? Thanks a lot. The following is the code: # Step 1: fit the glm clotting <- data.frame( u =
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
2009 Nov 02
2
a prolem with constrOptim
Hi, I apologize for the long message but the problem I encountered can't be stated in a few lines. I am having some problems with the function constrOptim. My goal is to maximize the likelihood of product of K multinomials, each with four catagories under linear constraints on the parameter values. I have found that the function does not work for many data configurations. #The likelihood
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,
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
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 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,
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]]
2009 Jul 19
1
trouble using optim for maximalisation of 2-parameter function
Hello, I am having trouble using "optim". I want to maximalise a function to its parameters [kind of like: univariate maximum likelihood estimation, but i wrote the likelihood function myself because of data issues ] When I try to optimize a function for only one parameter there is no problem: llik.expo<-function(x,lam){(length(x)*log(lam))-(length(x)*log(1-exp(-1*lam*
2004 Aug 09
4
linear constraint optim with bounds/reparametrization
Hello All, I would like to optimize a (log-)likelihood function subject to a number of linear constraints between parameters. These constraints are equality constraints of the form A%*%theta=c, ie (1,1) %*% 0.8,0.2)^t = 1 meaning that these parameters should sum to one. Moreover, there are bounds on the individual parameters, in most cases that I am considering parameters are bound between zero
2011 Jun 14
1
Using MLE Method to Estimate Regression Coefficients
Good Afternoon, I am relatively new to R and have been trying to figure out how to estimate regression coefficients using the MLE method. Some background: I am trying to examine scenarios in which certain estimators might be preferred to others, starting with MLE. I understand that MLE will (should) produce the same results as Ordinary Least Squares if the assumption of normality holds. That
2007 Jan 06
2
negative binomial family glm R and STATA
Dear Lister, I am facing a strange problem fitting a GLM of the negative binomial family. Actually, I tried to estimate theta (the scale parameter) through glm.nb from MASS and could get convergence only relaxing the convergence tolerance to 1e-3. With warning messages: glm1<-glm.nb(nbcas~.,data=zonesdb4,control=glm.control(epsilon = 1e-3)) There were 25 warnings (use warnings() to see
2008 Sep 16
0
Maximum likelihood estimation of a truncated regression model
Hi, I have a quick question regarding estimation of a truncation regression model (truncated above at 1) using MLE in R. I will be most grateful to you if you can help me out. The model is linear and the relationship is "dhat = bhat0+Z*bhat+e", where dhat is the dependent variable >0 and upper truncated at 1; bhat0 is the intercept; Z is the independent variable and is a uniform