Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.3K Views
|
12 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How do I ReplacePart conditionally?

POSTED BY: Roger J Brown
12 Replies

Hi Roger,

I think you consequently should use StandardDeviation not Variance.

data// Transpose // 
   Map[With[{r = DeleteCases[#, "x"]}, # /. 
       "x" :> RandomVariate[NormalDistribution[Mean@r, StandardDeviation@r]]] &] // 
  Transpose // TableForm

Robert

POSTED BY: Robert Nowak
Posted 3 years ago
POSTED BY: Eric Rimbey
Posted 3 years ago
POSTED BY: Eric Rimbey
Posted 3 years ago

Here's a possible approach:

(*Find where all the "x" are. Since the replacement will depend on \
position, we can use these positions later.*)
Position[data, "x"]

(*You'll need some function that uses the position to determine a \
replacement value. This is just a placeholder for this example*)
ColumnBasedFn

(*Here's a function that uses the position to do a replacement on the \
data*)
TheReplacer[data_, position_] := 
 ReplacePart[data, position -> ColumnBasedFn[Last@position]]

(*And finally, we apply the replacer with our positions. There are \
many ways to do this, I just happened to choose Fold.*)
Fold[TheReplacer, data, Position[data, "x"]]

This produces:

{{12, 27, 19, 44}, {11, ColumnBasedFn[2], 13, 43}, {ColumnBasedFn[1], 
  56, 23, 60}, {15, 37, 19, 52}, {10, 31, 21, ColumnBasedFn[4]}, {18, 
  40, 22, 43}, {15, 50, 28, 56}, {21, ColumnBasedFn[2], 
  ColumnBasedFn[3], 46}, {20, 39, 25, 51}, {ColumnBasedFn[1], 36, 23, 
  56}, {ColumnBasedFn[1], 27, 21, 39}, {14, 32, 20, 46}, {22, 40, 
  ColumnBasedFn[3], 49}, {ColumnBasedFn[1], 31, ColumnBasedFn[3], 
  44}, {ColumnBasedFn[1], 18, 21, ColumnBasedFn[4]}, {16, 23, 21, 
  34}, {27, 26, 23, 59}, {17, ColumnBasedFn[2], ColumnBasedFn[3], 
  35}, {13, 21, 20, 48}, {11, 51, 18, 54}}
POSTED BY: Eric Rimbey

Hi Roger

you could handle the problem of insufficient number of non "x" elements like this:

data // Transpose // 
   Map[With[{r = DeleteCases[#, "x"]}, # /. 
       "x" :> If[Length@r >= 2, RandomVariate[NormalDistribution[Mean@r, StandardDeviation@r]], 
         If[Length@r == 1, r[[1]], 0]]] &] // Transpose // TableForm

If a column has just one non "x" element left, return it as rv if it has no elements return 0 as rv.

Robert

POSTED BY: Robert Nowak

I had already made that change since that is the second argument in NormalDistribution[ ] that the documentation calls for.

POSTED BY: Roger J Brown

That fixed it. Thanks Your last comment is a weakness in my system so far. I have, for simplicity in order to get my head around the problem, assumed at least two non-missing elements in each row. Had not thought about the same affecting columns. Datasets with 19 of the 20 obs missing probably would be eliminated for other reasons before getting this far?

POSTED BY: Roger J Brown

Hi Roger

ok, just replace -> by :>

data// Transpose // 
   Map[With[{r = DeleteCases[#, "x"]}, # /. 
       "x" :> RandomVariate[NormalDistribution[Mean@r, Variance@r]]] &] // 
  Transpose // TableForm

Further you have to take care, that there remain at least two non "x" elements in each column.

Robert

POSTED BY: Robert Nowak

Tighter code, but all the rvs in a particular column must be differently, even though from the same dist.

POSTED BY: Roger J Brown

Hi Roger

data//Transpose//
  Map[With[{r = DeleteCases[#, "x"]},
     # /. "x" -> RandomVariate[NormalDistribution[Mean@r, Variance@r]]]&]//
 Transpose

Robert

POSTED BY: Robert Nowak

Brilliant. It took me 5 minutes to finish it off. Notebook attached in case you are interested. I particularly appreciate your exposition explaining the code. Often help here (still good of course) just sends the code. But, as the saying goes "If you give a man a fish..."

Thanks. I will probably be back. This is part of a larger project to work out Wolfram code for Full Information Likelihood which I do not believe exists at present.

Attachments:
POSTED BY: Roger J Brown

@Eric - That works. Thanks for your help.

But rather than x I now have "ColumnBasedFn[x]" where I once had "x"

I feel like I am very close. Attached notebook shows the current situation (earlier file had a problem and has been replaced with one that opens cleanly)

Attachments:
POSTED BY: Roger J Brown
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard