here is a hint: each time you run your OLS function in the for loop, you
need to store the relevant estimates. a matrix might be helpful.
miya
On Wed, Feb 11, 2009 at 7:55 PM, Natasha Khouri <natasha.khouri at gmail.com>wrote:
Hi Again,
Thanks Miya: returning a list is working for my OLS function. However,
when I am looking my OLS function to be run on 100 random draws, I'm still
seeing no output. Also, one thing I am wondering about is since I run my
OLS function off the data set itself (and not on a set of pre-defined
variables y,x1,x2, and x3), these x's and y are created for the purposes of
the function but aren't stored on the console so I can pull them up again,
for example when I use ls() . Is this a problem that could be related to
why I'm not seeing output when OLS is looping 100 times? Two sections of
code below:
OLS runs on one variable and extracts the four itself:
OLS<-function(data){
#extract independent and dependent variables
x<-as.matrix(cbind(int=1,data["x1"],data["x2"],data["x3"]))
y<-as.matrix(data["y"])
And my for loop which I don't think is doing anything:
for(i in 1:100){
#create an index of 50 numbers from 1 to 1000 named "ind"
#make a new subset of data which pulls the rows named in ind
ind<-sample(1:1000,50,replace=T)
mysamp<-data[ind,]
#run OLS on the new sample of 50
OLS(mysamp)
}
Thanks,
Natasha
------------------------------
*From:* gov2001-l-bounces at
lists.fas.harvard.edu [mailto:
gov2001-l-bounces at
lists.fas.harvard.edu] *On Behalf Of *Miya Woolfalk
*Sent:* Wednesday, February 11, 2009 7:26 PM
*To:* gov2001-l at
lists.fas.harvard.edu
*Subject:* Re: [gov2001-l] basic question output
you can use the following syntax to return multiple items:
return( list(object1,object2,etc) )
On Wed, Feb 11, 2009 at 7:21 PM, Natasha Khouri <natasha.khouri at gmail.com>
wrote:
Hi All,
I **think** my code is doing the right thing, but I can't control my
output. I've tried all kinds of things to get R to spit out betas, standard
errors, etc, as it goes through my OLS function. The only thing I've been
able to do is get it to output *one* of the created variables, such as
beta, per function, with this:
#defining OLS function?.
?
+ out<-b
+ return(out)
+ }
#run OLS function:
OLS(data)
int x1 x2 x3
4.0426412 5.9869415 -2.9962049 0.2485113
And if I change the variable out to be defined as "b.standarderror",
I'll
get the standard error but nothing else. It doesn't work to create multiple
outs. This is a bigger problem when I'm running my for-loop 100 times, and
no output is being generated at all.
Thanks for your help,
Natasha
------------------------------
*From:* gov2001-l-bounces at
lists.fas.harvard.edu [mailto:
gov2001-l-bounces at
lists.fas.harvard.edu] *On Behalf Of *Lauren Peritz
*Sent:* Wednesday, February 11, 2009 7:01 PM
*To:* gov2001-l at
lists.fas.harvard.edu
*Subject:* Re: [gov2001-l] basic question on Beta-hat formula
Hi Everyone,
Thanks for the helpful comments. I'm still having difficulty getting my
OLS function to work. I seem to be getting the same error as Tom, in which
my definition of the beta vector produces a "> Lapack routine dgesv: system
is exactly singular" error message.
This message relates to my use of the "solve(t(matx)%*%matx)" part of the
code.
Do you think the problem is not the syntax here but instead the way I
define matx with a first column of 1's?
I've pasted the code below and would be grateful for any advice you can
offer!
Best wishes,
Lauren
###Section 2.1#####
OLS <-function(matx,vectY) {
matx<- cbind(matrix(1,nrow(matx),1),matx) #make a column of 1s having x
entries and bind to first row of x matrix
beta <- solve(t(matx) %*% matx) %*% t(matx) %*% vectY #calculate beta =
(XT*X)^-1*XT*y
u <- vectY-(matx%*%beta) # calculate error using residuals of vectorY minus
(x time matrixbeta)
sigmasq<-(t(u)%*%u)/(nrow(vectY)-(ncol(matx))) #create sigma squared = u'
times u
return(beta,sigmasq,u)
}
#####################
On Wed, Feb 11, 2009 at 10:57 AM, Z. A. Townsend <ztownsend at gmail.com>
wrote:
Tom-
I have betas <- solve(t(X) %*% X) %*% t(X) %*% y
and it seems to work fine. My suggestion would be to change your
parentheses around (t(x)%*%y). Your order of operations might be a
little screwed up.
On Wed, Feb 11, 2009 at 10:42 AM, Thomas Sander <tom_sander at harvard.edu>
wrote:
> I didn't take the statistics class last semester, but various folks have
> suggested that Gov2001 is the right course for me on inference.
> In trying to compute the
coefficients, I've been first appending a column
of
1s at the start of the X data. Then after
appending these 1s, I run the
formula for Beta-hat, when I invert (solve) for X-prime times X it tells
me
> either that
> Error in solve.default...: system is computationally singular: reciprocal
> condition number = 2.09476e-18
> or
> Lapack routine dgesv: system is exactly singular
> I've been trying to set the
coefficients to
> coeff<-solve(t(x)%*%x)%*%(t(x)%*%y)
> There must be something obviously
wrong with the formula since it can't
even
> solve a really simple regression problem I give it, to say nothing of the
> PS1_Data file.
> Any ideas of what's wrong with
this formula?
> Many thanks.
> Tom
_______________________________________________
gov2001-l mailing list
>
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
--
Miya Woolfalk
Ph.D. Student
Harvard University
Government and Social Policy
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
--
Miya Woolfalk
Ph.D. Student
Harvard University
Government and Social Policy