Quantcast

some R exercises

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

some R exercises

UdVBIA
This post was updated on .
Hi!

I tried everything to avoid asking somebody but I have absolutely no clue how to solve this tasks. I'll be really glad if someone could explain that to me...

1.



Well, the first part is of course no problem but I have really trouble with the tasks i) and ii). just don't know how to write the function and why I should use rexp...

2.


Oh I love loops. Well - "I think" I have to solve the first task to solve this...

Well there is more but I'll tried to fight with it a few more hours.

Thanks for helping!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: some R exercises

Florian Weiler
On Exercise 5, how about this (I'm rather new to R as well, so no guarantee this is right and I'm sure there are more efficient ways to do this!):

fun1 <- function(x) {
  y <- 0
  for (i in 1:length(x))
      {y[i] <- (x[i]-mean(x))^2}
  sum(y)/(length(x)+1)
}

fun2 <- function(x) {
  y <- 0
  for (i in 1:length(x))
  {y[i] <- (x[i]-mean(x))^2}
  sum(y)/(length(x))
}

x <- rexp(15)
fun1(x)
fun2(x)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: some R exercises

Florian Weiler
Just thought about it, you can have this of course "cheaper" without the loops. Like this:

fun1 <- function(x) sum((x-mean(x))^2)/(length(x)+1)
fun2 <- function(x) sum((x-mean(x))^2)/(length(x))

x <- rexp(15)
fun1(x)
fun2(x)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: R exercises - too confusing for me.

Kenn Konstabel
In reply to this post by UdVBIA
Hi,

But what if your professor reads this list?

Anyway, who cares, two hints:

1. It might be useful to read the explanations provided with the
problem. For example, your exercise tells you that "use the help
command ?rexp to read how to generate exponentially distributed
variables". On a careful reading, that may explain why they want you
to use rexp. Perhaps they want you to generate an exponentially
distributed variable?

2. If you're given an exercise on a topic that is not covered %in%
c(lectures, required.readings) and you can't figure it out by
yourself, maybe it would be useful to do some background reading
before you go on. You can find free books on R at
http://cran.r-project.org/other-docs.html . Or you could try
complaining the gray beard about asking you to do things he hasn't
taught you ... and then see what happens next.

Best regards,

K

On 5/9/12, UdVBIA <[hidden email]> wrote:

> Hi!
>
> I tried everything to avoid asking somebody but I have absolutely no clue
> how to solve this tasks. I'll be really glad if someone could explain that
> to me...
>
> 1.
>
> http://r.789695.n4.nabble.com/file/n4619850/task1.gif
>
> Well, the first part is of course no problem but I have really trouble with
> the tasks i) and ii). just don't know how to write the function and why I
> should use rexp...
>
> 2.
> http://r.789695.n4.nabble.com/file/n4619850/task2.gif
>
> Oh I love loops. Well - "I think" I have to solve the first task to solve
> this...
>
> Well there is more but I'll tried to fight with it a few more hours.
>
> Thanks for helping!
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/R-exercises-too-confusing-for-me-tp4619850.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Loading...