• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I get the averages of groups and the SSW within a 2d double jagged array

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please help me with my java coding.I have a 2d double jagged array (named jag), which contains 17rows(people id) and 19 columns(bloodlevels). I have calculated the average of each column and calculated the sum squared total.This works ok. My problem, is that the people are also arranged into groups, which is set as 5,6,6 in the coding below. So I need to write code to get me the averages of the individual groups in every columns and then calculate the sum squared within.

what we know is that for each one of the bloodlevel measurements (19) there are 17 patients results, and the 17 patients fall into 3 groups, the group sizes being (5,6,6). So using the theory(below) given to me, how do I code to get the averages of the groups in every bloodlevel measurement

the logic is that these groups have say a different genetic variation to the next group etc etc,

Below is the theory behind getting the groups averages but I do not know how to work the theory into coding. Any advice would be great. Apologies, I am not proficient in programming. I have no background in programming, I am from medical perspective.So if this makes no sense, just ask me a question. Thanks

Have a for-loop that runs from 0 up to num_groups (call this variable i). Using the group_sizes array you've already created, inside this i for-loop you create a second for loop (call it j) that adds up all the group_sizes entries from 0 up to i-1, ie sum = groupsizes[0]+groupsizes[1]+...+groupsizes[i-1], then you create two variables, lowerlimit = sum and upperlimit sum+groupsizes[i]. Once the j-for-loop has complete you create another for loop (inside the i loop still, but not inside the j loop), call this one k, which goes (for int k=lowerlimit; k<upperlimit; k++). Inside this k loop you can calculate mean of the i-th patient group and the ssw for the i-th patient group.






 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see a couple of things:

The array group_sizes is not initialized to anything. From what I understand you need to put the values 5, 6, and 6 into it. Do you know how to declare and initialize an array all at once?

As I read the instructions, the k loop should be below the j loop.

I don't see where the jag array is declared.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it important that you stick exactly to the proposed solution? If I were to tackle this problem, I would first get rid of all the arrays and make classes to represent the concepts in your description.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I hope people aren't going around calling that a jagged array; it appears to have rows the same length, so most people might call that a rectangular array. Also there is nothing 2D about “2D” arrays. In fact they are arrays or arrays, otherwise you could only declare rectangular arrays.
Were you told to use the name jag? Unless you are within 20 miles of St Mungo's Cathedral, you shouldn't use jag for anything associated with blood levels
Stephan is right: those multiple arrays are an accident waiting to happen.
 
Lucy Gallagher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have setup the {5,6,6} earlier and jag is setup as well. Sorry but we need to stick the arrays.

Does anyone have solutions on how to tackle this please

And I realise now jag was not a good name lol
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't really understand what it is they want you to do, and the algorithm they proposed how to do it is full of mistakes. Do you have a formal problem description?
 
Lucy Gallagher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,

So essentially, we are to carry out Anova analysis on data. So we read in a file(txt) and coverted everything into a 2d jagged array.

From this I calculated: the averages of each blood level for all the patients,

Then calculated the SST (sum of squares total)

Next I have to get have the averages of each of the groups in the bloodlevels

Then calculate SSW (sum squares within)
this is calculated by taking the groupaverage for each group away from every measurement in that group, then square them and then add them together, (again this is done for all 19)

But how to code this is my pain........

Apologies the loop earlier was me trying to do this... but failing misery

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it will be helpful to post the code you have done so far.
 
Lucy Gallagher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Well, Lucy, very difficult to step in because you wrote so much code and gone too far along with issues actually. With many issues. Sorry.

1. Personally myself I have never seen such amount of arrays in one class and don't see necessity for most of them.
2. Too many cryptic variable names.

No, that won't work. Do you have instruction given by your instructor? Could you post them here please. If these are numbered by tasks, please post only 1 or 2 tasks instructions.

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may have to start again, and only do a few tasks at a time. I suggest, for the time being, copy the array of arrays into your code and try the calculations on it one by one. It would help if you can find some application which you know will give you the correct results, so you can see whether something is going wrong.
As Liutauras said, please post the actual algorithms but not in the form of code; then we can see whether you are coding them wrongly. It would help if we knew what the variable names mean; if things beginning df mean degrees of freedom, call them degreesFreedomXXX or similar.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant about copying the array is to save you having to read it from the file. When you have the calculations working, you can put the file reading back.

I have had a look at your last method and find it very confusing. You appear to have two loops nested, and in one of them you appear to be calculating f repeatedly. You are not doing anything with any arrays, only reading them. I don't know what f is; it appears to be MSB &div MSW. If M is “mean”, how are you calculating those means? In the last method, you are not dividing any means by each other, only individual elements of the arrays. I suggest you write a method which averages all the elements of an array. Or their squares. There is a standard way to average arrays with low‑level code:-I shall leave you to work out how to change that to meanSquare. It is very easy. And don't use Math#pow for squares. That is one of the few places where you are allowed low‑level code and it works better than high‑level code. I would use high‑level code throughout; you will need to import DoubleStream.A DoubleStream does all the iteration you need for a number of doubles. You can create it from an array (I think) with its of() method. As you iterate it, you use the map() method to create another double stream which includes the square (d × d). The average() method returns an OptionalDouble with the average (if any) as its solitary field. The getAsDouble() method will throw an Exception if the Stream is empty. But you will already have thrown an Exception for an empty array in line 5, so if you ever get to line 10, you can be confident of getting a result.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have java (8) at my disposal at the moment, so can't test anything.
But Campbells suggestion is the right direction.

First of all: if you are in these kind of calculations, my advice would be: use R.

But lacking this, my ideas are: (and you need javva 8 AND your professor should allow it)

1) make a method that converts a say double[] array into a Map<Integer, List<Double>>

Here, the integer is the group number, and the list contains the elements of the array within that group.

2) create a method that takes a List<Double> as input and delivers a DoubleSummaryStatistics

Combining 1) and 2) gives you easyily the statistics you are looking for.

And by varying this grouping, it is also easy to get these data for all of your observations.

3) a nice method would be one that converts a double[] intio a List<Double>. I'm not sure if a DoubleStream.of works with an array.

4) since you are working with a 2D array, it is easy to get the individual rows. However, from what you write it seems that you must also work with colums. Now, to my lnowledge, that is not so handy in java. So it would seem that a method would come in handy that, given a 2D array, delivers its transposed array. Mind you, that would only make sense if your 2D array is rectangular. If so, then working with a column would then simply be working with a row of the transposed.

I have some working code for this at home, but here in office I don't have java.

But I'm sure that if all this sounds reasonable, then folks around here will guide you further.

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get columns from an array of arrays, you can make a Stream from the big array:-PS has some good suggestions there. You can find out about converting things to Maps here.
Yes, you can create a DoubleStream with the of method and an array; I tried it last night.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Lucy,

well, at home I made a beautiful java 8 framework, but when I was finished I realised it was far too "java 8" to do you any good.
And also, this "DoubleSummaryStatistics" does not keep track of squared values...

So, here is a suggestion to simplify your code nevertheless. Say, you have this class (My Analisys Of Variance):


Now, having this, it is then easy to write a method like:


and, to allow for easy grouping, say:



As an example of what I mean: you could use: getStatistics(array[i], 0, 5, 10, array[i].length - 1);

And creating a transposed matrix to get easy acces to the columns, seems handy.
 
reply
    Bookmark Topic Watch Topic
  • New Topic