By using R: Write a function which generates data from a linear model. Your func
ID: 3174550 • Letter: B
Question
By using R:
Write a function which generates data from a linear model. Your function should accept three arguments: the number of observations, a vector of betas, and the standard deviation of the noise. The standard deviation should have a default value of 1. Your function should check that the standard deviation satisfies any necessary constraints. Check that beta has at least one entry. Each entry of the design matrix should be i.i.d. Uniform between -1 and 1.
Your response vector must be equal to the beta vector times the design matrix plus mean zero, i.i.d. normal noise with the appropriate standard deviation. Your function must return a data frame. The first variable should be the response, named y. The remaining columns should be named x1 through xp where p=length(betas). You should use the paste0 command to create the names for these columns.
Explanation / Answer
R function as per the requirement is given below.
generate.data.lm <- function(n,beta.vector=c(),noise.sd=1) {
p <- length(beta.vector)
if (p==0) {
print("There should be non-zero length of beta vectors")
return
}
design.matrix <- matrix(,nrow = 0,ncol = 3)
for (i in 1:n) {
row.vector <- c(1,runif(p-1,min=-1,max=1)) # 1st element of the row vector should be 1 for intercept
design.matrix <- rbind(design.matrix,row.vector)
}
error.term <- rnorm(n,mean = 0,sd=noise.sd)
beta.matrix <- matrix(data = beta.vector,nrow = p,ncol = 1)
error.matrix <- matrix(data = error.term,nrow = n,ncol = 1)
y <- (design.matrix %*% beta.matrix) + error.matrix
for (j in 1:p) colname[j] <- paste0("x",j)
d <- data.frame(y,design.matrix)
names(d) <- c("y",colname)
d
}
An example to run the function.
> generate.data.lm(10,c(1,2,3),4)
y x1 x2 x3
1 2.7155163 1 0.4106457 -0.2471077
2 -2.0414607 1 -0.1520643 0.5709587
3 -1.3930556 1 -0.9030218 0.1495824
4 -2.8544394 1 -0.7777024 0.4410097
5 -0.1076148 1 0.8983147 0.3934416
6 -1.8662982 1 0.8366762 -0.1351742
7 3.0267816 1 0.5530055 0.7022182
8 -4.2281781 1 0.5203765 0.2519051
9 -0.3526499 1 -0.6456267 -0.8806701
10 5.9647228 1 0.8013693 0.4689525
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.