QuantStudentR
QuantStudentR

Reputation: 31

Function to Create Y-Values

I was hoping someone might be able to help me make sense of a homework question. I am not looking for a solution, mind you, just wondering if anyone would be able to explain the question a bit more simply for me, as I am new to data analysis and enrolled in an R class which had no prerequisites, but feel a bit lost with some of the language. Any help would be greatly appreciated!

So, the first part of the question was to create an array and fill it with random numeric data, which I did here:

question <- array( 1:1000, dim= c(25,4,1000))
colnames(question)<- c('x1','x2','x3','x4')

Now, the second part asks me to "write a function to create y-values," which should be a "linear combination" of the four variables. The example given is

y = 2 ∗ x1 + 5 ∗ x2 − 3 ∗ x3 + 0.7 ∗ x4 + RandomError.

The question adds that the result should be a matrix with dimensions of 25 × 1000. I am not sure what exactly this is asking or how to approach this problem. All I have so far, which I know is very little is

apply(question,c(1,3),sum)
function (y){ ...

Can anyone offer any guidance or clarification? Thank you so much!

Upvotes: 0

Views: 56

Answers (1)

C_Z_
C_Z_

Reputation: 7806

First of all, to make (pseudo)random numbers, you can use the rnorm function. That is, if you want to make 1000 random numbers that are normally distributed with mean of 0 and sd of 1, you can do rnorm(1000) (However, your array ends up being length 10000, so maybe you actually want to do rnorm(10000)).

Now, you should have an array question with dimensions 25 x 4 x 1000. You want to create a matrix y which combines four "slices" in question of size 25 x 1000 to create a matrix y of size 25 x 1000. You want to write a function f that will take all four "slices" of array question and combine them into one slice. You also want to incorporate random error, which again can be accomplished with the rnorm function.

For a simple example, let's make an array x with dimensions(10,2,10)

x = array(rnorm(200), dim = c(10,2,10))

And now let's write a function f that will add the two "slices" of x together.

f = function(my_array){
    my_array[,1,] + my_array[,2,]
    }

Let's execute the function on our array

y = f(x)
dim(y)

Hopefully you can expand this basic example to fit your case.

Upvotes: 1

Related Questions