Running a regression in R and formatting output

# Thanks to Bill Sundstrom for the mtable formatting code

# input data into R
data <- read.table(“tenureinsecurity.csv”, header=TRUE, sep=”,”)

# Tell R to assume survey1 is dataset from now until detach
attach(data)

# Run two regressions
fit1 <- lm(yield ~  female+age+fieldsize1)
fit2 <- lm(yield ~  female+age+fieldsize1+tenure)

# Put regression results into a table and label appropriately
fitx <- mtable(“Model 1″=fit1,”Model 2″=fit2,summary.stats=c(“sigma”,”R-squared”,”F”,”p”,”N”) )
(fitx <- relabel(fitx,
“(Intercept)” = “Constant”,
tenure = “Tenure insecurity”,
fieldsize1 = “Field size – hect.”,
female = “Is female?”,
age = “Age of farmer”
))

# Produces output in tab-delimited format
# Notepad window opens up in Windows, in Mac an R window opens; The contents can be pasted into Word or Excel
file123 <- “mtable123.txt”
write.mtable(fitx,file=file123)
file.show(file123)

About mkevane

Economist at Santa Clara University and Director of Friends of African Village Libraries.
This entry was posted in Politics. Bookmark the permalink.