[R] Error: invalid type (closure) for the variable 'time' - object specific trend

David Winsemius dwinsemius at comcast.net
Sat May 13 16:40:36 CEST 2017


> On May 13, 2017, at 4:07 AM, Tobias Christoph <s3tochri at uni-bayreuth.de> wrote:
> 
> Hey Peter,
> 
> thank you. Yes, I want to have "year" in the varibale.
> But if I use "*town*year*" as a furmula, R will create new factor 
> variable with n levels, where n = (num of towns) x (num of years). What 
> I'm trying to do is create 50 (town x year) variables such that 
> town1xyear is 1,2,3... when town== 1 and zero otherwise, repeat for 
> town2xyear, where state == 2, etc.
> 
> It is now clear? Sorry for my bad explanations.

I had suggested that you must provide str(R_Test_log_Neu). I'm still suggesting this would be a good idea.

Since you have not done so, we can only guess at the right course to follow from your reports of problems and errors. Peter pointed out that the `time` function was in the 'stats' package (not from plm or elsewhere as I imagined). You are implying that 'year' is currently a factor value with levels that appears as the character versions of integers.

You may be able to get closer to what is possible by using:

plm( log(revenue) ~ log(supply) + factor(town) + as.numeric(as.character(year)), 
     data=R_Test_log_Neu)

This should fix the problem noted by Peter and avoid the potentially incorrect construction of the desired linear trend.

If you used the interaction operator "*" between 'town' and the numeric version of 'year' it will give you two sets of coefficients involving 'town'. The first set will be the mean deviations from the base factor level. The other set will be the differences in slopes for the time trends for each of the (factored) towns from the overall time trend/slope. And for your data you wouldbe constructing a saturated model ... as you observed in your first message (which remains in the copied thread below).

-- 
David.

> 
> Toby
> 
> 
> Am 12.05.2017 um 23:23 schrieb peter dalgaard:
>>> On 12 May 2017, at 16:40 , Tobias Christoph <s3tochri at uni-bayreuth.de> wrote:
>>> 
>>> Hey guys,
>>> 
>>> thanks a lot for your tips. The regression is finally running. As you
>>> said, I had to integrate the column "year" in the function "time" in R.
>>> 
>>> So I used the following formula: *plm(log(revenue) ~ log(supply) +
>>> factor(town)*time(year), data=R_Test_log_Neu)*
>> Um, that might not do what I think you think it does. time() gives you the "vector of times at which a time series was sampled". If you feed it any regular vector, it just gives the numbers 1:n, witness
>> 
>>> time(rnorm(20))
>>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>> attr(,"tsp")
>> [1]  1 20  1
>> 
>> I suspect you just want "year" in the formula.
>> 
>>> So I have now sucessfully added a linear trend to my regression model?
>>> Another question that concernes me is how to add a quadratic trend
>>> instead of a linear trend. Can I just square the column "year"?
>> In principle, yes, but as others have pointed out, centering the variable may be a good idea, for numerical stability.
>> 
>> -pd
>> 
>>> Enjoy your weekend,
>>> 
>>> Toby
>>> 
>>> _My results see below:_
>>> 
>>> Balanced Panel: n=11, T=12, N=132
>>> 
>>> Residuals :
>>>     Min.  1st Qu.   Median  3rd Qu.     Max.
>>> -0.09610 -0.02370 -0.00152  0.01980  0.14000
>>> 
>>> Coefficients :
>>>                             Estimate Std. Error t-value  Pr(>|t|)
>>> log(supply)               -0.0080702  0.0133675 -0.6037  0.547411
>>> factor(town)2:time(year)  -0.0063245  0.0053744 -1.1768  0.242100
>>> factor(town)3:time(year)   0.0295522  0.0056776  5.2050 1.053e-06 ***
>>> factor(town)4:time(year)   0.0062191  0.0054152  1.1485  0.253549
>>> factor(town)5:time(year)   0.0159028  0.0054954  2.8939  0.004681 **
>>> factor(town)6:time(year)   0.0237112  0.0055395  4.2804 4.316e-05 ***
>>> factor(town)7:time(year)   0.0410007  0.0055734  7.3565 5.576e-11 ***
>>> factor(town)8:time(year)   0.0239085  0.0053751  4.4480 2.271e-05 ***
>>> factor(town)9:time(year)   0.0242342  0.0056855  4.2625 4.619e-05 ***
>>> factor(town)10:time(year)  0.0105890  0.0053302  1.9866  0.049733 *
>>> factor(town)11:time(year)  0.0095270  0.0056354  1.6906  0.094065 .
>>> ---
>>> Signif. codes:  0 �***� 0.001 �**� 0.01 �*� 0.05 �.� 0.1 � � 1
>>> 
>>> Total Sum of Squares:    0.46388
>>> Residual Sum of Squares: 0.2001
>>> R-Squared:      0.56863
>>> Adj. R-Squared: 0.4292
>>> F-statistic: 11.8637 on 11 and 99 DF, p-value: 7.3065e-14
>>> 
>>> 
>>> 
>>> Am 11.05.2017 um 23:17 schrieb David L Carlson:
>>>> What Rui said, but as important, you have four columns in your data called "town", "year", "revenue", and "supply". You do not have a column called "time".
>>>> 
>>>> -------------------------------------
>>>> David L Carlson
>>>> Department of Anthropology
>>>> Texas A&M University
>>>> College Station, TX 77840-4352
>>>> 
>>>> -----Original Message-----
>>>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Rui Barradas
>>>> Sent: Thursday, May 11, 2017 3:36 PM
>>>> To: Tobias Christoph <s3tochri at uni-bayreuth.de>; Duncan Murdoch <murdoch.duncan at gmail.com>; r-help at r-project.org
>>>> Subject: Re: [R] Error: invalid type (closure) for the variable 'time' - object specific trend
>>>> 
>>>> Hello,
>>>> 
>>>> A closure is, like you say, a function.
>>>> At an R prompt try:
>>>> 
>>>>> typeof(time)
>>>> [1] "closure"
>>>> 
>>>> So like Duncan suggested rename 'time', for instance capitalize it
>>>> 'Time'. That should do it.
>>>> 
>>>> Hope this helps,
>>>> 
>>>> Rui Barradas
>>>> 
>>>> 
>>>> 
>>>> Em 11-05-2017 21:20, Tobias Christoph escreveu:
>>>>> Hey Duncan,
>>>>> 
>>>>> thank you very much for your quick reply.
>>>>> 
>>>>> _My data used:_
>>>>> 
>>>>> 1st column(town):1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,...........,11
>>>>> 
>>>>> 2nd column(year):1,2,3,4,5,6,7,8,9,10,11,12,1,2,3.......,12
>>>>> 
>>>>> 3rd column (revenue):
>>>>> 
>>>>> 4th colum (supply):
>>>>> 
>>>>> I have now renamed my colums and did the regression again. Now there is
>>>>> a problem with R-squared, as it is the sum of 1 now with no given std.
>>>>> error and t-value. This is probably due to the fact, that I try to
>>>>> estimate more parameters than data.
>>>>> 
>>>>> To add a linear trend I found the following formular:*lm(y ~ x1 +
>>>>> factor(ccode)*time, data=df)*
>>>>> 
>>>>> I try to I decode it for and use it for my regression: *plm(log(revenue)
>>>>> ~ log(supply) + factor(town)*time, data=R_Test_log_Neu)*
>>>>> 
>>>>> When I do this regression I will get the original error: "invalid type
>>>>> (closure) for the variable 'time' - object specific trend"
>>>>> 
>>>>> With the notation"time" not my colum is meant, but probably the command
>>>>> "time" in R.
>>>>> 
>>>>> Can you follow my thoughts?
>>>>> 
>>>>> Tobi
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> Am 11.05.2017 um 17:23 schrieb Duncan Murdoch:
>>>>>> Duncan Murdoch
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> 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.
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> 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.
>>>> 
>>> 
>>> 	[[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list