I am trying to import data from Stata to R and fit a survival model. I did the following:
library(haven)
data <- read_dta("C:/Users/user/Desktop/data.dta")
View(data)
install.packages(c("survival", "survminer"))
library("survival")
library("survminer")
It worked well. However, I got errors:
data("data")
Warning message:
In data("data") : data set ‘data’ not found
fit <- survfit(Surv(data$finaltime, data$GSTATUS_DTHCNS_KI) , data = data)
Error in survfit.Surv(Surv(data$finaltime, data$GSTATUS_DTHCNS_KI), data = data) :
the survfit function requires a formula as its first argument
I wonder if you can tell me how to fix this.
The issue is you aren't supplying a formula. As noted in the documentation for survfit one must now supply a formula:
Older releases of the code also allowed the specification for a
single curve to omit the right hand of the formula, i.e.,
survfit(Surv(time, status)), in which case the formula argument is not
actually a formula. Handling this case required some non-standard and
fairly fragile manipulations, and this case is no longer supported.
Here in an example of a fix, where ~ 1 would be replaced by the formula that fits your research question:
fit <- survfit(Surv(data$finaltime, data$GSTATUS_DTHCNS_KI) ~ 1 , data = data)
summary(fit)
See help("survfit.formula") for more information.
Related
Trying to come up with a nested IF statement that works within a specified limit (battery state of charge) taking the necessary inputs over time.
[Sample Data Sheet]
My attempt to formulate for the State of charge operation is given below
= IF(AND(D5+A6-B6>=0.2*$G$1, D5+A6-B6<=0.95*$G$1), D5+A6-B6,0)
= IF(AND(D5+A6-B6<0.2*$G$1), D6= 0.2*$G$1+(D5+A6-B6).
= IF(AND(D5+A6-B6>0.95*$G$1), D6= 0.95*$G$1.
=IF(D5+A6-B6<=0.2*$G$1), 0.2*$G$1+(D5+A6-B6), IF(AND(D5+A6-B6>=0.2*$G$1, D5+A6-B6<=0.95*$G$1), D5+A6-B6,0), IF(D5+A6-B6>=0.95*$G$1), 0.95*$G$1)))
=IF(D5+A6-B6>=0.95*$G$1), 0.95*$G$1, IF(AND(D5+A6-B6>=0.2*$G$1, D5+A6-B6<=0.95*$G$1), D5+A6-B6,0), IF(D5+A6-B6<=0.2*$G$1), 0.2*$G$1+D5+A6-B6)))
I keep getting errors that my formulae are incorrect, please help for both state of charge and spare generation equations.
Thanks
Below is the link to the sample sheet that I have created.
https://drive.google.com/file/d/1kmhkBybMg18Odrow5jElcEt8RcLXqCFH/view?usp=sharing
I'm Assuming the G1 value is 1 or more than 1. Also, I'm also assuming the formula is for E5 cell. Here is the proposed solution.
=IF((D5+A6-B6)<(0.2*$G$1),(0.2*$G$1)+(D5+A6-B6),IF((D5+A6-B6)>(0.95*$G$1), (0.95*$G$1), IF(AND((D5+A6-B6)>=(0.2*$G$1), (D5+A6-B6)<=(0.95*$G$1)), (D5+A6-B6),"not in range")))
Here is the same equation expanded ( to see the logic and sequence ) :
=IF((D5+A6-B6)<(0.2*$G$1) , (0.2*$G$1)+(D5+A6-B6),
IF((D5+A6-B6)>(0.95*$G$1) , (0.95*$G$1),
IF(AND( (D5+A6-B6)>=(0.2*$G$1),(D5+A6-B6)<=(0.95*$G$1)) , (D5+A6-B6),"not in range"
)
)
)
Please have a check.
I was trying to convert a match object to a string in perl6. The method Str on a match object is defined as:
method Str(Match:D: --> Str:D)
I would think I could use Str($match) to accomplish this. And it seems to convert it to a string, but I'm getting an error using the string with the following code:
my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = Str($match);
say $test1.WHAT;
say $test1;
With the output:
(Match)
(Str)
With the error:
Cannot find method 'gist': no method cache and no .^find_method in
block at .code.tio line 6
However, if I run:
my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = $match.Str;
say $test1.WHAT;
say $test1;
I get no error and the result:
(Match)
(Str)
rudolph
Is this a bug or me misunderstanding how it works?
Thanks for reading.
I'm writing this up as an answer even though it's actually an incomplete discussion of a bug, so not at all normal SO fare. The alternative of lots of comments doesn't seem better.
It's a bug. Perhaps you just golfed this.
dd $test1; instead of say $test1; is helpful in that it displays BOOTStr $test1 = (BOOTStr without .perl method).
Based on that I searched the rakudo repo for BOOTStr and that led to the above issue.
Golfing it further leads to:
say $ = Str(Match.new);
Note that these are all fine:
say Str(Match.new);
say $ = Int(Match.new);
say $ = Str(Date.new: '2015-12-31');
It appears to be a combination of leaking some implementation details regarding how Rakudo/NQP/MoarVM bootstrap; Match being an NQP object; Str() on that being wonky; and assigning it to a Scalar container (the $ is an anonymous one) making that wonkiness visible.
I'll add more when/if I figure it out.
I am trying to get some variables and numbers out from an Excel table using Matlab.
The variables below named "diffZ_trial1-4" should be calculated by the difference between two columns (between "start" and "finish"). However I get the error:
Undefined operator '-' for input arguments of type"
'cell'.
And I have read somewhere that it could be related to the fact that I get {} output instead of [] and maybe I need to use cell2mat or convert the output somehow. But I must have done that wrongly, as it did not work!
Question: How can I calculate the difference between two columns below?
clear all, close all
[num,txt,raw] = xlsread('test.xlsx');
start = find(strcmp(raw,'HNO'));
finish = find(strcmp(raw,'End Trial: '));
%%% TIMELINE EACH TRIAL
time_trial1 = raw(start(1):finish(1),8);
time_trial2 = raw(start(2):finish(2),8);
time_trial3 = raw(start(3):finish(3),8);
time_trial4 = raw(start(4):finish(4),8);
%%%MOVEMENT EACH TRIAL
diffZ_trial1 = raw(start(1):finish(1),17)-raw(start(1):finish(1),11);
diffZ_trial2 = raw(start(2):finish(2),17)-raw(start(2):finish(2),11);
diffZ_trial3 = raw(start(3):finish(3),17)-raw(start(3):finish(3),11);
diffZ_trial4 = raw(start(4):finish(4),17)-raw(start(4):finish(4),11);
You are right, raw contains data of all types, including text (http://uk.mathworks.com/help/matlab/ref/xlsread.html#outputarg_raw). You should use num, which is a numeric matrix.
Alternatively, if you have an updated version of Matlab, you can try readtable (https://uk.mathworks.com/help/matlab/ref/readtable.html), which I think is more flexible. It creates a table from an excel file, containing both text and numbers.
I have been using tensorflow to implement a Convolutional neural network,
I have a requirement that the the output values be less than a given value MAX_VAL
I tried creating a matrix filled with MAX_VAL and then using tf.select and tf.greater :
filled = tf.fill(output.get_shape(),MAX_VAL)
modoutput = tf.select(tf.greater(output, filled), filled, output)
But this doesn't work because the shape of output is not known statically:
It is [?, 30] and tf.fill requires an explicit shape.
Any idea how do i implement this?
There is an alternative solution that uses tf.fill() like your initial version. Instead of using Tensor.get_shape() to get the static shape of output, use the tf.shape() operator to get the dynamic shape of output when the step runs:
output = ...
filled = tf.fill(tf.shape(output), MAX_VAL)
modoutput = tf.select(tf.greater(output, filled), filled, output)
(Note also that the tf.clip_by_value() operator might be useful for your purposes.)
I figured out a way to do it.
Instead of using tf.fill I used tf.ones_like
filled = MAX_VAL*tf.ones_like(output)
modoutput = tf.select(tf.greater(output, filled), filled, output)
Please mention if there is a faster or better way to do this is possible.
I have a variable that is created by a loop. The variable is large enough and in a complicated enough form that I want to save the variable each time it comes out of the loop with a different name.
PM25 is my variable. But I want to save it as PM25_year in which the year changes based on `str = fname(13:end)'
PM25 = permute(reshape(E',[c,r/nlay,nlay]),[2,1,3]); % Reshape and permute to achieve the right shape. Each face of the 3D should be one day
str = fname(13:end); % The year
% Third dimension is organized so that the data for each site is on a face
save('PM25_str', 'PM25_Daily_US.mat', '-append')
The str would be a year, like 2008. So the variable saved would be PM25_2008, then PM25_2009, etc. as it is created.
Defining new variables based on data isn't considered best practice, but you can store your data more efficiently using a cell array. You can store even a large, complicated variable like your PM25 variable within a single cell. Here's how you could go about doing it:
Place your PM25 data for each year into the cell array C using your loop:
for i = 1:numberOfYears
C{i} = PM25;
end
Resulting in something like this:
C = { PM25_2005, PM25_2006, PM25_2007 };
Now let's say you want to obtain your variable for the year 2006. This is easy (assuming you aren't skipping years). The first year of your data will correspond to position 1, the second year to position 2, etc. So to find the index of the year you want:
minYear = 2005;
yearDesired = 2006;
index = yearDesired - minYear + 1;
PM25_2006 = C{index};
You can do this using eval, but note that it's often not considered good practice. eval may be a security risk, as it allows user input to be executed as code. A better way to do this may be to use a cell array or an array of objects.
That said, I think this will do what you want:
for year = 2008:2014
eval(sprintf('PM25_%d = permute(reshape(E',[c,r/nlay,nlay]),[2,1,3]);',year));
save('PM25_Daily_US.mat',sprintf('PM25_%d',year),'-append');
end
I do not recommend to set variables like this since there is no way to track these variables and completely prevents all kind of error checking that MATLAB does beforehand. This kind of code is handled completely in runtime.
Anyway in case you have a really good reason for doing this I recommend that you use the function assignin for this.
assignin('caller', ['myvar',num2str(1)], 63);