On Tue, Feb 10, 2009 at 10:07 AM, Thomas Sander <tom_sander at harvard.edu>wrote:
Additional questions to the ones below in my earlier
e-mail.
Is there a formula I can use for computing the b intercept under problem
2.2 or a R command I can use. I created an OLS function in 2.1 and am using
that in 2.2, but my OLS function in 2.1 doesn't produce a b intercept, only
a residual. If you give me the formula for b intercept, I'll code it in R.
The intercept isn't really derived from any formula. It is simply another
parameter, just like beta_1 or beta_2. In your code, you cbinded a column
of 1s to your X matrix. The column of 1s corresponds to the beta_0
intercept term. That is, we multiply beta_1 by x_1 and beta_2 by x_2, and
we multiply beta_0 by 1. So the intercept should be the first value in your
beta vector.
And I thought that in the handouts there was instructions about how to name
parts of a dataset (like naming columns, etc.). I couldn't find this in the
notes and couldn't find online help in R on this using the '?' command. I
haven't been able to load the PS1_Data yet, but can you tell me where to
find out about how to name the parts of the data as I'll need to define this
as X and Y for my OLS function.
If you are working with a dataframe, you can use colnames(data) or
names(data). For a matrix, you can use colnames(matrix). So if I wanted to
name my columns in my dataset, I can do
colnames(my.data) <- c("y", "x1", "x2", "x3")
Sorry for the *getting up to speed* questions.
Thanks. Tom
This is coding that I have
#2.1
OLS<-function(x,y) {
x<-cbind(matrix(1,nrow(x),1),x) # ADD COLUMN of 1s TO X
coeff<-solve(t(x)%*%x)%*%(t(x)%*%y) #make coeff equal to X-prime times X
times x-prime times y
u<-y-(x%*%coeff) # Create the error term from the residuals of y-Beta times
x
sigmahatsq<-(t(u)%*%u)/(nrow(y)-(ncol(x))) #create sigma hat-squared =
u-prime times u over n-k+1
# note since I had already added a column of 1s to x I used number of
columns of x, no x+1
return(coeff,sigmahatsq,u)
}
#2.2 run OLS and compute intercept on PS1_Data.RData
setwd(dir="C:/Documents and Settings/tsander/My
Documents1/Mdrivebkp/E2001")
load(PS1_Data.RData)
data
head(data)
tail(data)
summary(data)
#name y as the y part of data
#name x as the explanatory variables part of data
OLS(x,y)
# compute intercept NEED TO GET FORMULA
*Thomas Sander*
Executive Director
Saguaro Seminar: Civic Engagement in America
Harvard Kennedy School, Harvard University
79 JFK Street, Taubman 364, Cambridge, MA 02138
T - 617.496-2764 F - 617.495.1589
tom_sander at
harvard.edu
www.hks.harvard.edu/saguaro
new - socialcapitalblog <http://socialcapital.wordpress.com/>
*Please consider the environment before printing this e-mail*
----- Forwarded by Thomas Sander/FS/KSG on 02/10/2009 09:57 AM -----
*Thomas Sander/FS/KSG*
02/10/2009 09:19 AM
To
plam at
fas.harvard.edu
cc
Subject
Couple of other questions Patrick --
A couple of questions:
1) I can't seem to figure out how to use the Histogram function. I tried
?Hist and didn't really understand the documentation, perhaps because I
don't have a clear sense of what par(mfrow=c(5,5)) is doing. Is there a
better place to look to understand examples of how to specify what it is
doing a histogram for 1.2.2 but I'm clearly missing something
I have
show<-par(mfrow=c(5,5))
for (i in 1:25) {
hist(show[i,])
}
2) I can't seem to figure out how to load in the PS1_Data.
I set the wd to be where I keep the data and then loaded the data
setwd(dir="C:/Documents and Settings/tsander/My
Documents1/MDrivebkp/E2001")
load(PS1_Data)
I also tried load(PS1_Data.RData)
but in both cases, while it accepts the setwd command, it gives me an error
on the load command:
Error in load(PS1_Data.RData) : object "PS1_Data.RData" not found
Any advice?
3) finally, is there a command to count whether there are odd items in a
vector? I have kludgy code that sees whether the length of a vector divided
by 2 and then truncated is not equal to the lenth of a vector divided by 2,
but there must be a more elegant way to do this, and my code doesn't seem to
be working.
Many thanks.
Best.
Tom
*Thomas Sander*
Executive Director
Saguaro Seminar: Civic Engagement in America
Harvard Kennedy School, Harvard University
79 JFK Street, Taubman 364, Cambridge, MA 02138
T - 617.496-2764 F - 617.495.1589
tom_sander at
harvard.edu
www.hks.harvard.edu/saguaro
new - socialcapitalblog <http://socialcapital.wordpress.com/>
*Please consider the environment before printing this e-mail*
--
Patrick Lam
Department of Government and Institute for Quantitative Social Science,
Harvard University
http://www.people.fas.harvard.edu/~plam<http://www.people.fas.harvard.ed…