Solving R Homework: Sunspots Data

In summary: That's impressive. In my high school programming class we had a project to write a program on a TI-84 that finds the area under the curve using Riemann Sums. It finds the left sum, right sum, midpoint sum, trapezoidal sum and the definite integral. I still use it to this day...In summary, the assignment requires the user to do the following:1. Obtain the sample mean and sample variance of sunspots data.2. For 10000 replications, randomly sample n sunspots observations from the given dataset.3. Obtain juxtaposed plots of the histograms of the means corresponding to n=10 and n=100.4. Plot the
  • #1
_N3WTON_
351
3

Homework Statement


I have an upcoming assignment for a Statistics/Probability class that requires me to write a program in R. The assignment requires me to do the following:
1. Obtain the sample mean x and sample variance s2 of the sunspots data.
2. Provide a histogram of the data.
3. For 10000 replications, randomly sample n sunspots observations from the given dataset. For each replication, obtain the sample mean. That is, you will have 10000 sample means. Compute the sample variance of these sample means.
4. Repeat the above process with n=10, 20, 30, 40, 50, 60, 70, 80, 90, and 100.
5. Obtain juxtaposed plots of the histograms of the means corresponding to n=10 and n=100.
6. Plot the variances as the function of n. What are your observations?

Homework Equations

The Attempt at a Solution


This is the code I have come up with thus far:
Code:
filename <- "C://Users//Colin//Desktop//Project1Data.txt"
data <- read.table(filename)
colnames(data) <- c("id","x")

#Questions 1 and 2
x <- data$x
mean(x)
var(x)
summary(data)
hist(x)

#partial answer to Questions 3 and 4
p <- numeric()

for (i in 1:10000){
s <- data[sample(1:1053,10,replace=FALSE),]
x <- s$x
y[i] <- mean(x)
p[i] <- y[i]
assign(paste("sample",i,sep=""),s)
assign(paste("mean",i,sep=""),y)
}
Unfortunately this code is generating a number of errors that I am unsure how to deal with (I was hoping somebody here could help). I suppose I should begin with the first error I am receiving: "
Warning message:
In mean.default(x) : argument is not numeric or logical: returning NA". Any help or advice would be greatly appreciated, thanks.
 
Physics news on Phys.org
  • #2
Can you print the x values? That might give you a clue.

When you read in the flle the data object has three columns so data$x is an array of values. The question is are they strings or numbers. The mean() is looking for numbers.
 
  • #3
jedishrfu said:
Can you print the x values? That might give you a clue.

When you read in the flle the data object has three columns so data$x is an array of values. The question is are they strings or numbers. The mean() is looking for numbers.
Here is a sample of the data set:
"x" "id"

"1" 33

"2" 81

"3" 7

"4" 38

"5" 113

"6" 92

"7" 18

"8" 24

"9" 100

"10" 89

"11" 14

"12" 26

"13" 19

"14" 32

"15" 7

"16" 58

"17" 1

"18" 30

"19" 41

"20" 32
the "x" values go until 1053 so I don't want to post them all here, but I have it saved in the above format in a .txt file
 
  • #4
Okay you can see right there that the x value is a string not a number so that's why its failing. I think they want you to average the second column which is numeric. It makes no sense to find the mean of x here since it is just a row counter.
 
  • #5
I see a mismatch you defined your columns as id and x whereas your data says x and id. Try switching the column labeling in your program at line 3
 
  • #6
Thank you for that. However, I'm a little confused as to why I am getting results for variance but not mean despite the code being the same for both...
 
  • #7
I apologize if some of these questions are sort of elementary, my only real knowledge of R comes from a crash course a few days ago from a tutorial I found online :/
 
  • #9
  • #10
Don't feel,bad we all start programming somewhere and that means we get tripped up by some very simple things. My first programming at my high school on a fancy programmable desktop calculator and I couldn't figure out how to turn it on. The teacher had a chuckle but was impressed with my first program to compute the nth root of any number.
 
  • #11
jedishrfu said:
Don't feel,bad we all start programming somewhere and that means we get tripped up by some very simple things. My first programming at my high school on a fancy programmable desktop calculator and I couldn't figure out how to turn it on. The teacher had a chuckle but was impressed with my first program to compute the nth root of any number.
That's impressive. In my high school programming class we had a project to write a program on a TI-84 that finds the area under the curve using Riemann Sums. It finds the left sum, right sum, midpoint sum, trapezoidal sum and the definite integral. I still use it to this day :D
 
  • #12
My project was on a very limited desktop calculator circa 1970 that had programmable features for math only. You were really limited in what it could do and how much memory it had. In my program, I ran out of registers and so I had hit the enter key repeatedly for the next iteration because I had no register for the loop counter. It used something akin to the Newton approximation technique optimized for the machine.
 
  • #13
jedishrfu said:
You were really limited in what it could do and how much memory it had.
I always forget how spoiled we are today: for most basic programs memory really doesn't enter into the equation at all. I have a lot of respect for people good at programming, it takes a ton of patience. Right now I'm about ready to throw my computer out a window into the snow because I can't get this program working haha :D
 
  • #14
Learn how to use the print statement. It's one of the best debugging tools in a new environment like this. Don't trust your code write a few lines and test them and eventually you'll get through it.
 
  • #15
jedishrfu said:
Learn how to use the print statement. It's one of the best debugging tools in a new environment like this. Don't trust your code write a few lines and test them and eventually you'll get through it.
Thanks again for the advice. After a lot of trying I was able to successfully complete this assignment a few minutes ago :D
 
  • #16
That's great. Welcome to the programmers guild!
 

1. What is the purpose of solving R Homework for Sunspots Data?

The purpose of solving R Homework for Sunspots Data is to analyze and understand patterns and trends in sunspot activity over a period of time. This data can provide insights into the behavior of the sun and its potential impacts on Earth's climate and technology.

2. How do I import the Sunspots Data into R?

To import the Sunspots Data into R, you can use the "read.csv()" function or the "read.table()" function. These functions allow you to read data from a csv file or a text file, respectively. You can also use the "import()" function from the "foreign" package to import data from other statistical software.

3. What are some common methods for analyzing Sunspots Data in R?

Some common methods for analyzing Sunspots Data in R include time series analysis, linear regression, and forecasting techniques. You can also use data visualization tools such as ggplot2 to create plots and charts to better understand the data.

4. Can I use R packages to assist in solving Sunspots Data Homework?

Yes, there are several R packages that can assist in solving Sunspots Data Homework. Some popular packages include "forecast" for time series analysis, "lmtest" for linear regression, and "ggplot2" for data visualization. It is always recommended to explore and utilize relevant packages to make your analysis more efficient and accurate.

5. How can I improve my skills in solving Sunspots Data Homework using R?

One of the best ways to improve your skills in solving Sunspots Data Homework using R is to practice regularly and work on different datasets. You can also join online communities or attend workshops and seminars to learn from others and stay updated on new techniques and tools. Additionally, reading books and tutorials on R programming and data analysis can also help in enhancing your skills.

Similar threads

  • Precalculus Mathematics Homework Help
Replies
1
Views
1K
  • Precalculus Mathematics Homework Help
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
475
  • Set Theory, Logic, Probability, Statistics
Replies
28
Views
2K
  • Precalculus Mathematics Homework Help
Replies
1
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
838
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
964
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
17
Views
2K
Back
Top