Muhammad Bilal
2016-Apr-08 18:57 UTC
[R] Generating random data with non-linear correlation between two variables
Hi All, I am new to R and don't know how to achieve it. I am interested in generating a hypothetical dataframe that is consisted of say two variables named v1 and v2, based on the following constraints: 1. The range of v1 is 500-1500. 2. The mean of v1 is say 1100 3. The range of v2 is 300-950. 4. The mean of v2 is say 400 5. There exists a positive trend between these two variables, meaning that as v1 increases, v2 be also increase. 6. But the trend should be slightly non-linear. i.e., curved line. Is it possible to automatically generate through functions like rnorm. Any help will be highly appreciated. Many Thanks and Kind Regards -- Muhammad Bilal Research Assistant and Doctoral Researcher, Bristol Enterprise, Research and Innovation Centre (BERIC), University of the West of England (UWE), Frenchay Campus, Bristol, BS16 1QY muhammad2.bilal at live.uwe.ac.uk<mailto:olugbenga2.akinade at live.uwe.ac.uk> [[alternative HTML version deleted]]
hd625b
2016-Apr-08 22:43 UTC
[R] Generating random data with non-linear correlation between two variables
Hello, I changed the last 3 lines from v1 and v2 to myData$v1 and myData$v2. Creating the vectors in not a problem. Where you will run into issues is when you create the data.frame. The length of v1 is 1001 and the length of v2 is 651. The warning message below is telling us that the lengths of v1 and v2 are different. "Shorter vectors are recycled as often as need be until they match the length of the longest vector." See section 2.2 of this document for more on recycling. https://cran.r-project.org/doc/manuals/R-intro.pdf v1 <- sort(rnorm(500:1500, mean = 1100, sd = 1), decreasing = F) v2 <- sort(rnorm(300:950, mean = 400, sd = 1), decreasing = F) length(v1) length(v2) myData <- data.frame(cbind(v1, v2)) Warning message: In cbind(v1, v2) : number of rows of result is not a multiple of vector length (arg 2) plot(myData$v1, myData$v2, type = "l") cor(myData$v1, myData$v2, method = "pearson") plot(myData, type = 'l') Michael Long On 04/08/2016 11:57 AM, Muhammad Bilal wrote:> Hi All, > > I am new to R and don't know how to achieve it. > > I am interested in generating a hypothetical dataframe that is consisted of say two variables named v1 and v2, based on the following constraints: > 1. The range of v1 is 500-1500. > 2. The mean of v1 is say 1100 > 3. The range of v2 is 300-950. > 4. The mean of v2 is say 400 > 5. There exists a positive trend between these two variables, meaning that as v1 increases, v2 be also increase. > 6. But the trend should be slightly non-linear. i.e., curved line. > > Is it possible to automatically generate through functions like rnorm. > > Any help will be highly appreciated. > > Many Thanks and > > Kind Regards > > -- > Muhammad Bilal > Research Assistant and Doctoral Researcher, > Bristol Enterprise, Research and Innovation Centre (BERIC), > University of the West of England (UWE), > Frenchay Campus, > Bristol, > BS16 1QY > > muhammad2.bilal at live.uwe.ac.uk<mailto:olugbenga2.akinade at live.uwe.ac.uk> > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
Rolf Turner
2016-Apr-09 03:46 UTC
[R] [FORGED] Generating random data with non-linear correlation between two variables
On 09/04/16 06:57, Muhammad Bilal wrote:> Hi All, > > I am new to R and don't know how to achieve it. > > I am interested in generating a hypothetical dataframe that is consisted of say two variables named v1 and v2, based on the following constraints: > 1. The range of v1 is 500-1500. > 2. The mean of v1 is say 1100 > 3. The range of v2 is 300-950. > 4. The mean of v2 is say 400 > 5. There exists a positive trend between these two variables, meaning that as v1 increases, v2 be also increase. > 6. But the trend should be slightly non-linear. i.e., curved line. > > Is it possible to automatically generate through functions like rnorm. > > Any help will be highly appreciated.This sounds to me very much like a homework problem. We don't do people's homework for them on this list. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Muhammad Bilal
2016-Apr-09 09:57 UTC
[R] [FORGED] Generating random data with non-linear correlation between two variables
Hi Rolf Turner, Firstly, I really appreciate your help. Is it possible to give the number of entries I need so that rnorm function generates only that number of rows? Also is it possible to generate the points with spread randomly such that they don't overlap each other too much. Many Thanks and Kind Regards -- Muhammad Bilal Research Assistant and PhD Student, Bristol Enterprise, Research and Innovation Centre (BERIC), University of the West of England (UWE), Frenchay Campus, Bristol, BS16 1QY muhammad2.bilal at live.uwe.ac.uk ________________________________________ From: Rolf Turner <r.turner at auckland.ac.nz> Sent: 09 April 2016 04:46 To: Muhammad Bilal Cc: r-help at r-project.org Subject: Re: [FORGED] [R] Generating random data with non-linear correlation between two variables On 09/04/16 06:57, Muhammad Bilal wrote:> Hi All, > > I am new to R and don't know how to achieve it. > > I am interested in generating a hypothetical dataframe that is consisted of say two variables named v1 and v2, based on the following constraints: > 1. The range of v1 is 500-1500. > 2. The mean of v1 is say 1100 > 3. The range of v2 is 300-950. > 4. The mean of v2 is say 400 > 5. There exists a positive trend between these two variables, meaning that as v1 increases, v2 be also increase. > 6. But the trend should be slightly non-linear. i.e., curved line. > > Is it possible to automatically generate through functions like rnorm. > > Any help will be highly appreciated.This sounds to me very much like a homework problem. We don't do people's homework for them on this list. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Muhammad Bilal
2016-Apr-09 10:08 UTC
[R] [FORGED] Generating random data with non-linear correlation between two variables
No its not. I am doing all these experiments for my own learning purpose. I am Oracle SQL & PLSQL programmer and I can do these things with Oracle analytical functions. However at present I am keen to learn R, with no other interest right now. Thanks -- Muhammad Bilal Research Assistant and PhD Student, Bristol Enterprise, Research and Innovation Centre (BERIC), University of the West of England (UWE), Frenchay Campus, Bristol, BS16 1QY muhammad2.bilal at live.uwe.ac.uk ________________________________________ From: Rolf Turner <r.turner at auckland.ac.nz> Sent: 09 April 2016 04:46 To: Muhammad Bilal Cc: r-help at r-project.org Subject: Re: [FORGED] [R] Generating random data with non-linear correlation between two variables On 09/04/16 06:57, Muhammad Bilal wrote:> Hi All, > > I am new to R and don't know how to achieve it. > > I am interested in generating a hypothetical dataframe that is consisted of say two variables named v1 and v2, based on the following constraints: > 1. The range of v1 is 500-1500. > 2. The mean of v1 is say 1100 > 3. The range of v2 is 300-950. > 4. The mean of v2 is say 400 > 5. There exists a positive trend between these two variables, meaning that as v1 increases, v2 be also increase. > 6. But the trend should be slightly non-linear. i.e., curved line. > > Is it possible to automatically generate through functions like rnorm. > > Any help will be highly appreciated.This sounds to me very much like a homework problem. We don't do people's homework for them on this list. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Possibly Parallel Threads
- How to convert XML file to R Data Frame?
- How to access the latitude & longitude for UK post codes in R
- [FORGED] Generating random data with non-linear correlation between two variables
- [FORGED] Generating random data with non-linear correlation between two variables
- [FORGED] Generating random data with non-linear correlation between two variables